1. 程式人生 > >搖一搖的實現

搖一搖的實現

模擬微信搖一搖的動畫效果和震動。分三布來實現:

1 佈局xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#111"
>
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" > <!-- 這裡我們讓那葵花圖片居中 --> <ImageView android:id="@+id/shakeBg" android:layout_width
="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/shakehideimg_man2" />
<!-- 然後手的上半部分和下半部分覆蓋原來的葵花照片,同樣也是居中顯示--> <LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical" >
<RelativeLayout android:id="@+id/shakeImgUp" android:layout_width="fill_parent" android:layout_height="190dp" android:background="#111"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:src="@drawable/shake_logo_up" /> </RelativeLayout> <RelativeLayout android:id="@+id/shakeImgDown" android:layout_width="fill_parent" android:layout_height="190dp" android:background="#111"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:src="@drawable/shake_logo_down" /> </RelativeLayout> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/shake_title_bar" android:layout_width="fill_parent" android:layout_height="45dp" android:background="@drawable/title_bar" android:gravity="center_vertical" > <Button android:layout_width="70dp" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="返回" android:textSize="14sp" android:textColor="#fff" android:onClick="shake_activity_back" android:background="@drawable/title_btn_back"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搖一搖" android:layout_centerInParent="true" android:textSize="20sp" android:textColor="#ffffff" /> <ImageButton android:layout_width="67dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:src="@drawable/mm_title_btn_menu" android:background="@drawable/title_btn_right" android:onClick="linshi" /> </RelativeLayout> <SlidingDrawer android:id="@+id/slidingDrawer1" android:layout_width="match_parent" android:layout_height="match_parent" android:content="@+id/content" android:handle="@+id/handle" > <Button android:id="@+id/handle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/shake_report_dragger_up" /> <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f9f9f9" > <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitXY" android:src="@drawable/shake_line_up" /> </LinearLayout> </SlidingDrawer> </RelativeLayout>

效果如下:
這裡寫圖片描述

2 建立一個監聽器,並在裡面建立一個監聽介面,我們使用加速感測器來實現


/**
* 一個檢測手機搖晃的監聽器
*/
public class ShakeListener implements SensorEventListener {
// 速度閾值,當搖晃速度達到這值後產生作用
private static final int SPEED_SHRESHOLD = 3000;
// 兩次檢測的時間間隔
private static final int UPTATE_INTERVAL_TIME = 70;
// 感測器管理器
private SensorManager sensorManager;
// 感測器
private Sensor sensor;

// 重力感應監聽器介面,自己寫的
private OnShakeListener onShakeListener;
// 上下文
private Context mContext;

// 手機上一個位置時重力感應座標
private float lastX;
private float lastY;
private float lastZ;

// 上次檢測時間
private long lastUpdateTime;

// 構造器
public ShakeListener(Context c) {
    // 獲得監聽物件
    mContext = c;
    start();
}

// 開始
public void start() {
    // 獲得感測器管理器
    sensorManager = (SensorManager) mContext
            .getSystemService(Context.SENSOR_SERVICE);
    if (sensorManager != null) {
        // 獲得加速感測器Sensor.TYPE_ACCELEROMETER
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    }
    // 註冊
    if (sensor != null) {
        sensorManager.registerListener(this, sensor,
                SensorManager.SENSOR_DELAY_GAME);
    }

}

// 停止檢測
public void stop() {
    sensorManager.unregisterListener(this);
}

// 設定重力感應監聽器
public void setOnShakeListener(OnShakeListener listener) {
    onShakeListener = listener;
}

// 重力感應器感應獲得變化資料,實現SensorEventListener介面的方法
public void onSensorChanged(SensorEvent event) {
    // 現在檢測時間
    long currentUpdateTime = System.currentTimeMillis();
    // 兩次檢測的時間間隔
    long timeInterval = currentUpdateTime - lastUpdateTime;
    // 判斷是否達到了檢測時間間隔
    if (timeInterval < UPTATE_INTERVAL_TIME)
        return;
    // 現在的時間變成last時間
    lastUpdateTime = currentUpdateTime;

    // 獲得x,y,z座標
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];

    // 獲得x,y,z的變化值
    float deltaX = x - lastX;
    float deltaY = y - lastY;
    float deltaZ = z - lastZ;

    // 將現在的座標變成last座標
    lastX = x;
    lastY = y;
    lastZ = z;

    double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ
            * deltaZ)
            / timeInterval * 10000;
    Log.v("thelog", "===========log===================");
    // 達到速度閥值,發出提示
    if (speed >= SPEED_SHRESHOLD) {
        onShakeListener.onShake();
    }
}
//實現SensorEventListener介面的方法
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

// 搖晃監聽介面
public interface OnShakeListener {
    public void onShake();
}

}


3 搖一搖介面,動畫效果的實現

為上半部分的下半部分TranslateAnimation的動畫的平移和還原,必須注意上下動畫開始的時間、動畫演示的時間的一致性

public class ShakeActivity extends Activity{
//手機搖晃的監聽器類,自己寫的
private ShakeListener mShakeListener = null;
private Vibrator mVibrator;
private RelativeLayout mImgUp;
private RelativeLayout mImgDn;
private RelativeLayout mTitle;

private SlidingDrawer mDrawer;
private Button mDrawerBtn;

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.shake_activity);


    mVibrator = (Vibrator)getApplication().getSystemService(VIBRATOR_SERVICE);
    //搖一搖的上部分圖片
    mImgUp = (RelativeLayout) findViewById(R.id.shakeImgUp);
    //搖一搖的下部分圖片
    mImgDn = (RelativeLayout) findViewById(R.id.shakeImgDown);
    //搖一搖的標題
    mTitle = (RelativeLayout) findViewById(R.id.shake_title_bar);
    //上滑動
    mDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
    //上滑動下的按鈕
    mDrawerBtn = (Button) findViewById(R.id.handle);
    //滑動監聽,設定SlidingDrawer被開啟的事件處理 
    mDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener()
    {   public void onDrawerOpened()
        {   //將滑動按鈕設定向下的
            mDrawerBtn.setBackgroundResource(R.drawable.shake_report_dragger_down);
            //向上移動
            TranslateAnimation titleup = 
                    new TranslateAnimation(
                            Animation.RELATIVE_TO_SELF,0f,
                            Animation.RELATIVE_TO_SELF,0f,
                            Animation.RELATIVE_TO_SELF,0f,
                            Animation.RELATIVE_TO_SELF,-1.0f);
            titleup.setDuration(200);
            titleup.setFillAfter(true);
            mTitle.startAnimation(titleup);
        }
    });
     /* 設定SlidingDrawer被關閉的事件處理 */
    mDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener()
    {   public void onDrawerClosed()
        {   
            mDrawerBtn.setBackgroundResource(R.drawable.shake_report_dragger_up);
            TranslateAnimation titledn = 
                    new TranslateAnimation(
                            Animation.RELATIVE_TO_SELF,0f,
                            Animation.RELATIVE_TO_SELF,0f,
                            Animation.RELATIVE_TO_SELF,-1.0f,
                            Animation.RELATIVE_TO_SELF,0f);
            titledn.setDuration(200);
            titledn.setFillAfter(false);
            mTitle.startAnimation(titledn);
        }
    });

    //搖一搖監聽
    mShakeListener = new ShakeListener(this);

    mShakeListener.setOnShakeListener(new OnShakeListener() {

        public void onShake() {

            startAnim();  //開始 搖一搖手掌動畫
            mShakeListener.stop();
            startVibrato(); //開始 震動
            //開啟執行緒
            new Handler().postDelayed(new Runnable(){
                @Override
                public void run(){

                    Toast mtoast;
                    mtoast = Toast.makeText(getApplicationContext(),
                             "抱歉,暫時沒有找到\n在同一時刻搖一搖的人。\n再試一次吧!", 10);
                           mtoast.show();
                           mVibrator.cancel();
                           mShakeListener.start();
                }
            }, 2000);
        }
    });

}

//定義搖一搖動畫動畫
public void startAnim () { 
    //動畫效果集合
    AnimationSet animup = new AnimationSet(true);
    //移動效果,向上移動
    TranslateAnimation mytranslateanimup0 = 
            new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,-0.5f);
    //動畫時間
    mytranslateanimup0.setDuration(1000);

    //移動效果,向下移動
    TranslateAnimation mytranslateanimup1 = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF,0f,
            Animation.RELATIVE_TO_SELF,0f,
            Animation.RELATIVE_TO_SELF,0f,
            Animation.RELATIVE_TO_SELF,+0.5f);
    mytranslateanimup1.setDuration(1000);
    //設定動畫開始時間
    mytranslateanimup1.setStartOffset(1000);
    //新增動畫效果集合
    animup.addAnimation(mytranslateanimup0);
    animup.addAnimation(mytranslateanimup1);
    //搖一搖小手的上部分
    mImgUp.startAnimation(animup);


    //搖一搖小手的下部分動畫
    AnimationSet animdn = new AnimationSet(true);
    TranslateAnimation mytranslateanimdn0 = 
            new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,+0.5f);
    mytranslateanimdn0.setDuration(1000);
    TranslateAnimation mytranslateanimdn1 = 
            new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,-0.5f);
    mytranslateanimdn1.setDuration(1000);
    mytranslateanimdn1.setStartOffset(1000);
    animdn.addAnimation(mytranslateanimdn0);
    animdn.addAnimation(mytranslateanimdn1);
    mImgDn.startAnimation(animdn);  
}

//定義震動
public void startVibrato(){ 
     //第一個{}裡面是節奏陣列, 第二個引數是重複次數,-1為不重複,
    //非-1從pattern的指定下標開始重複
    mVibrator.vibrate( new long[]{500,200,500,200}, -1);
}

 //標題欄 返回按鈕
public void shake_activity_back(View v) {    
    this.finish();
  }  

//右側按鈕的監聽
public void linshi(View v) {     
    startAnim();
}  

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mShakeListener != null) {
        mShakeListener.stop();
    }
}

}
“`

效果如下:

這裡寫圖片描述