Android仿微信搖一搖功能
用到知識(shí)點(diǎn)
2.補(bǔ)間動(dòng)畫
3.手機(jī)震動(dòng) (Vibrator)
4.較短 聲音/音效 的播放 (SoundPool)
首先布局文件
android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/shake_bg"android:gravity="center"android:orientation="vertical" >android:layout_width="match_parent"android:layout_height="260dp"android:background="@color/shake_flower_bg"android:gravity="center"android:orientation="vertical" >android:id="@+id/shake_flower"android:layout_width="match_parent"android:layout_height="200dp"android:src="@drawable/shake_flower_img" />android:id="@+id/shake_loading"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/shakeImgDown"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:gravity="center"android:orientation="horizontal">android:layout_width="18dp"android:layout_height="18dp"android:indeterminateDuration="1100"android:indeterminateDrawable="@drawable/progressbar_shake_loading"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="正在搜尋美好"android:textSize="16dp"android:singleLine="true"android:ellipsize="end"android:layout_marginLeft="6dp"android:textColor="@color/white" />android:id="@+id/shake_result_layout"android:layout_width="match_parent"android:layout_height="80dp"android:layout_below="@+id/shakeImgDown"android:layout_marginLeft="40dp"android:layout_marginRight="40dp"android:layout_marginTop="10dp"android:background="@drawable/shake_result_bg"android:gravity="center_vertical"android:visibility="visible"android:padding="@dimen/listitem_top" >android:id="@+id/shake_result_img"android:layout_width="56dp"android:layout_height="56dp"android:layout_centerVertical="true"/>android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="@dimen/layout_padding"android:layout_toRightOf="@id/shake_result_img"android:gravity="center_vertical"android:orientation="vertical" >android:id="@+id/shake_result_txt_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="top"android:gravity="center_vertical"android:textSize="16dp"android:textColor="@color/shake_reslut_txt_color" />android:id="@+id/shake_result_txt_value"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="top"android:gravity="center_vertical"android:textColor="@color/shake_reslut_txt_color"android:textSize="16dp" />android:id="@+id/shakeImgUp"android:layout_width="match_parent"android:layout_height="130dp"android:background="@color/shake_bg" >android:layout_width="match_parent"android:layout_height="80dp"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:src="@drawable/shake_logo_up" />android:id="@+id/shakeImgUp_line"android:layout_width="match_parent"android:layout_height="4dp"android:layout_alignParentBottom="true"android:background="@color/black"android:visibility="gone" >android:layout_width="match_parent"android:layout_height="3dp"android:layout_gravity="bottom"android:background="@color/shake_line_bg" />android:id="@+id/shakeImgDown"android:layout_width="match_parent"android:layout_height="130dp"android:layout_below="@id/shakeImgUp"android:background="@color/shake_bg" >android:layout_width="match_parent"android:layout_height="80dp"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:src="@drawable/shake_logo_down" />android:id="@+id/shakeImgDown_line"android:layout_width="match_parent"android:layout_height="4dp"android:layout_alignParentTop="true"android:background="@color/black"android:visibility="gone" >android:layout_width="match_parent"android:layout_height="3dp"android:layout_gravity="top"android:background="@color/shake_line_bg" />????
首先實(shí)現(xiàn)搖一搖的操作
public class ShakeListener implements SensorEventListener {// 速度閾值,當(dāng)搖晃速度達(dá)到這值后產(chǎn)生作用private static final int SPEED_SHRESHOLD = 3000;// 兩次檢測(cè)的時(shí)間間隔private static final int UPTATE_INTERVAL_TIME = 70;// 傳感器管理器private SensorManager sensorManager;// 傳感器private Sensor sensor;// 重力感應(yīng)監(jiān)聽(tīng)器private OnShakeListenerCallBack onShakeListener;// 上下文private Context mContext;// 手機(jī)上一個(gè)位置時(shí)重力感應(yīng)坐標(biāo)private float lastX;private float lastY;private float lastZ;// 上次檢測(cè)時(shí)間private long lastUpdateTime;// 構(gòu)造器public ShakeListener(Context context) {// 獲得監(jiān)聽(tīng)對(duì)象mContext = context;start();}/**開(kāi)始重力傳感器的檢測(cè)*/public void start() {// 獲得傳感器管理器sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);if (sensorManager != null) {// 獲得重力傳感器sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);}// 注冊(cè)if (sensor != null) {sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);}}/**停止檢測(cè)*/public void stop() {sensorManager.unregisterListener(this);}/**重力感應(yīng)器感應(yīng)獲得變化數(shù)據(jù)*/@Overridepublic void onSensorChanged(SensorEvent event) {// 現(xiàn)在檢測(cè)時(shí)間long currentUpdateTime = System.currentTimeMillis();// 兩次檢測(cè)的時(shí)間間隔long timeInterval = currentUpdateTime - lastUpdateTime;// 判斷是否達(dá)到了檢測(cè)時(shí)間間隔if (timeInterval < UPTATE_INTERVAL_TIME)return;// 現(xiàn)在的時(shí)間變成last時(shí)間lastUpdateTime = currentUpdateTime;// 獲得x,y,z坐標(biāo)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;// 將現(xiàn)在的坐標(biāo)變成last坐標(biāo)lastX = x;lastY = y;lastZ = z;double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ) / timeInterval * 10000;// 達(dá)到速度閥值,發(fā)出提示if (speed >= SPEED_SHRESHOLD) {onShakeListener.onShake();}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}/**搖晃監(jiān)聽(tīng)接口*/public interface OnShakeListenerCallBack {public void onShake();}/**設(shè)置重力感應(yīng)監(jiān)聽(tīng)器*/public void setOnShakeListener(OnShakeListenerCallBack listener) {onShakeListener = listener;}}
搖一搖完成后動(dòng)畫
/** 搖一搖手掌上部分動(dòng)畫 */public AnimationSet getUpAnim(){AnimationSet animationSet = new AnimationSet(true);TranslateAnimation translateAnimation0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-0.8f);translateAnimation0.setDuration(OPEN_TIME);translateAnimation0.setAnimationListener(openAnimationListner);//圖片上移動(dòng)畫監(jiān)聽(tīng)TranslateAnimation translateAnimation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.8f);translateAnimation1.setDuration(CLOSE_TIME);translateAnimation1.setAnimationListener(closeAnimationListener);//圖片下移動(dòng)畫監(jiān)聽(tīng)translateAnimation1.setStartOffset(OFFSET_TIME);translateAnimation1.setInterpolator(new AccelerateInterpolator(1));animationSet.addAnimation(translateAnimation0);animationSet.addAnimation(translateAnimation1);return animationSet;}/** 搖一搖手掌下部分動(dòng)畫 */public AnimationSet getDownAnim(){AnimationSet animationSet = new AnimationSet(true);TranslateAnimation translateAnimation0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.8f);translateAnimation0.setDuration(OPEN_TIME);translateAnimation0.setAnimationListener(openAnimationListner);//圖片上移動(dòng)畫監(jiān)聽(tīng)TranslateAnimation translateAnimation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-0.8f);translateAnimation1.setDuration(CLOSE_TIME);translateAnimation1.setAnimationListener(closeAnimationListener);//圖片下移動(dòng)畫監(jiān)聽(tīng)translateAnimation1.setStartOffset(OFFSET_TIME);translateAnimation1.setInterpolator(new AccelerateInterpolator(1));animationSet.addAnimation(translateAnimation0);animationSet.addAnimation(translateAnimation1);return animationSet;}/** 顯示搖出結(jié)果動(dòng)畫 */public Animation getReusltAnim(){TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1,Animation.RELATIVE_TO_SELF,0);translateAnimation.setDuration(OPEN_TIME);translateAnimation.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {if (mDoAnimationListener != null) {mDoAnimationListener.onAnimEnd(SHAKE_RESULT_VISIBLE_CODE );}}@Overridepublic void onAnimationRepeat(Animation animation) {}});return translateAnimation;}/** 隱藏?fù)u出結(jié)果動(dòng)畫 */public Animation getReusltGoneAnim(){TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,3);translateAnimation.setDuration(OPEN_TIME);translateAnimation.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {if (mDoAnimationListener != null) {mDoAnimationListener.onAnimEnd(SHAKE_RESULT_GONE_CODE);}}@Overridepublic void onAnimationRepeat(Animation animation) {}});return translateAnimation;}/**手掌圖開(kāi)啟動(dòng)畫監(jiān)聽(tīng)*/public Animation.AnimationListener openAnimationListner = new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {//動(dòng)畫開(kāi)始執(zhí)行時(shí)回調(diào)if (mDoAnimationListener != null) {mDoAnimationListener.onAnimStart(OPEN_ANIM_CODE);}}@Overridepublic void onAnimationEnd(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}};/**手掌圖合上動(dòng)畫監(jiān)聽(tīng)*/public Animation.AnimationListener closeAnimationListener = new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {//動(dòng)畫結(jié)束時(shí)回調(diào)if (mDoAnimationListener != null) {mDoAnimationListener.onAnimEnd(COLSE_ANIM_CODE);}}@Overridepublic void onAnimationRepeat(Animation animation) {}};public interface DoAnimationListener{void onAnimStart(int code);void onAnimEnd(int code);}
震動(dòng)
震動(dòng)首先要加權(quán)限
震動(dòng)代碼
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);vibrator.vibrate(500);
接下來(lái)就是播放音效了
public class Sound {private static Sound mInstance;private SoundPool mSoundPool;//音效池private HashMapmSoundPoolMap;//定義一個(gè)HashMap用于存放音頻流的ID public static Sound getInstance() {if (mInstance == null) {mInstance = new Sound();}return mInstance;}/**初始化音效池---向音效池添加音效*/public void initPool(final Context context){mSoundPoolMap = new HashMap<>();mSoundPool = new SoundPool(3, AudioManager.STREAM_ALARM,1);if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {AudioAttributes audioAttributes = null;audioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_ALARM).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();mSoundPool = new SoundPool.Builder().setMaxStreams(3).setAudioAttributes(audioAttributes).build();} else { // 5.0 以前mSoundPool = new SoundPool(3, AudioManager.STREAM_ALARM, 0); // 創(chuàng)建SoundPool}new Thread(new Runnable() {@Overridepublic void run() {mSoundPoolMap.put(0,mSoundPool.load(context.getResources().openRawResourceFd(R.raw.shake_sound_male),1));mSoundPoolMap.put(1,mSoundPool.load(context.getResources().openRawResourceFd(R.raw.shake_match),1));mSoundPoolMap.put(2,mSoundPool.load(context.getResources().openRawResourceFd(R.raw.shake_nomatch),1));}}).start();}/** 播放搖一搖開(kāi)始聲音 */public void playStartSound(){try {mSoundPool.play(mSoundPoolMap.get(0),1,1,0,0,1.3f);} catch (Exception e) {e.printStackTrace();}}/** 播放搖一搖結(jié)束聲音 */public void playEndSound(){try {mSoundPool.play(mSoundPoolMap.get(1),1,1,0,0,1.0f);} catch (Exception e) {e.printStackTrace();}}/** 什么都沒(méi)搖到 */public void playNotingSound(){try {mSoundPool.play(mSoundPoolMap.get(2),1,1,0,0,1.0f);} catch (Exception e) {e.printStackTrace();}}}
以上幾個(gè)步驟實(shí)現(xiàn)整個(gè)搖一搖效果
最終代碼,動(dòng)畫部分
public class ShakeAnimation{/**展開(kāi)動(dòng)畫*/public static final long OPEN_TIME = 500;/**關(guān)合動(dòng)畫*/public static final long CLOSE_TIME = 280;/**開(kāi)合動(dòng)畫間隔*/public static final long OFFSET_TIME = 800;/**模擬延遲*/public static final long SIMULLATE_DELAY = 400;public static final int OPEN_ANIM_CODE = 1;public static final int COLSE_ANIM_CODE = 2;public static final int SHAKE_RESULT_GONE_CODE = 3;public static final int SHAKE_RESULT_VISIBLE_CODE = 4;private static DoAnimationListener mDoAnimationListener;private static ShakeAnimation needForAnim;public static ShakeAnimation getInstance(DoAnimationListener doAnimationListener) {mDoAnimationListener = doAnimationListener;if (needForAnim == null) {needForAnim = new ShakeAnimation();}return needForAnim;}/** 搖一搖手掌上部分動(dòng)畫 */public AnimationSet getUpAnim(){AnimationSet animationSet = new AnimationSet(true);TranslateAnimation translateAnimation0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-0.8f);translateAnimation0.setDuration(OPEN_TIME);translateAnimation0.setAnimationListener(openAnimationListner);//圖片上移動(dòng)畫監(jiān)聽(tīng)TranslateAnimation translateAnimation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.8f);translateAnimation1.setDuration(CLOSE_TIME);translateAnimation1.setAnimationListener(closeAnimationListener);//圖片下移動(dòng)畫監(jiān)聽(tīng)translateAnimation1.setStartOffset(OFFSET_TIME);translateAnimation1.setInterpolator(new AccelerateInterpolator(1));animationSet.addAnimation(translateAnimation0);animationSet.addAnimation(translateAnimation1);return animationSet;}/** 搖一搖手掌下部分動(dòng)畫 */public AnimationSet getDownAnim(){AnimationSet animationSet = new AnimationSet(true);TranslateAnimation translateAnimation0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.8f);translateAnimation0.setDuration(OPEN_TIME);translateAnimation0.setAnimationListener(openAnimationListner);//圖片上移動(dòng)畫監(jiān)聽(tīng)TranslateAnimation translateAnimation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-0.8f);translateAnimation1.setDuration(CLOSE_TIME);translateAnimation1.setAnimationListener(closeAnimationListener);//圖片下移動(dòng)畫監(jiān)聽(tīng)translateAnimation1.setStartOffset(OFFSET_TIME);translateAnimation1.setInterpolator(new AccelerateInterpolator(1));animationSet.addAnimation(translateAnimation0);animationSet.addAnimation(translateAnimation1);return animationSet;}/** 顯示搖出結(jié)果動(dòng)畫 */public Animation getReusltAnim(){TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1,Animation.RELATIVE_TO_SELF,0);translateAnimation.setDuration(OPEN_TIME);translateAnimation.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {if (mDoAnimationListener != null) {mDoAnimationListener.onAnimEnd(SHAKE_RESULT_VISIBLE_CODE );}}@Overridepublic void onAnimationRepeat(Animation animation) {}});return translateAnimation;}/** 隱藏?fù)u出結(jié)果動(dòng)畫 */public Animation getReusltGoneAnim(){TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,3);translateAnimation.setDuration(OPEN_TIME);translateAnimation.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {if (mDoAnimationListener != null) {mDoAnimationListener.onAnimEnd(SHAKE_RESULT_GONE_CODE);}}@Overridepublic void onAnimationRepeat(Animation animation) {}});return translateAnimation;}/**手掌圖開(kāi)啟動(dòng)畫監(jiān)聽(tīng)*/public Animation.AnimationListener openAnimationListner = new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {//動(dòng)畫開(kāi)始執(zhí)行時(shí)回調(diào)if (mDoAnimationListener != null) {mDoAnimationListener.onAnimStart(OPEN_ANIM_CODE);}}@Overridepublic void onAnimationEnd(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}};/**手掌圖合上動(dòng)畫監(jiān)聽(tīng)*/public Animation.AnimationListener closeAnimationListener = new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {//動(dòng)畫結(jié)束時(shí)回調(diào)if (mDoAnimationListener != null) {mDoAnimationListener.onAnimEnd(COLSE_ANIM_CODE);}}@Overridepublic void onAnimationRepeat(Animation animation) {}};public interface DoAnimationListener{void onAnimStart(int code);void onAnimEnd(int code);}}
音效部分
public class Sound {private static Sound mInstance;private SoundPool mSoundPool;//音效池private HashMapmSoundPoolMap;//定義一個(gè)HashMap用于存放音頻流的ID public static Sound getInstance() {if (mInstance == null) {mInstance = new Sound();}return mInstance;}/**初始化音效池---向音效池添加音效*/public void initPool(final Context context){mSoundPoolMap = new HashMap<>();mSoundPool = new SoundPool(3, AudioManager.STREAM_ALARM,1);if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {AudioAttributes audioAttributes = null;audioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_ALARM).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();mSoundPool = new SoundPool.Builder().setMaxStreams(3).setAudioAttributes(audioAttributes).build();} else { // 5.0 以前mSoundPool = new SoundPool(3, AudioManager.STREAM_ALARM, 0); // 創(chuàng)建SoundPool}new Thread(new Runnable() {@Overridepublic void run() {mSoundPoolMap.put(0,mSoundPool.load(context.getResources().openRawResourceFd(R.raw.shake_sound_male),1));mSoundPoolMap.put(1,mSoundPool.load(context.getResources().openRawResourceFd(R.raw.shake_match),1));mSoundPoolMap.put(2,mSoundPool.load(context.getResources().openRawResourceFd(R.raw.shake_nomatch),1));}}).start();}/** 播放搖一搖開(kāi)始聲音 */public void playStartSound(){try {mSoundPool.play(mSoundPoolMap.get(0),1,1,0,0,1.3f);} catch (Exception e) {e.printStackTrace();}}/** 播放搖一搖結(jié)束聲音 */public void playEndSound(){try {mSoundPool.play(mSoundPoolMap.get(1),1,1,0,0,1.0f);} catch (Exception e) {e.printStackTrace();}}/** 什么都沒(méi)搖到 */public void playNotingSound(){try {mSoundPool.play(mSoundPoolMap.get(2),1,1,0,0,1.0f);} catch (Exception e) {e.printStackTrace();}}}
重力傳感器部分
public class ShakeListener implements SensorEventListener {// 速度閾值,當(dāng)搖晃速度達(dá)到這值后產(chǎn)生作用private static final int SPEED_SHRESHOLD = 3000;// 兩次檢測(cè)的時(shí)間間隔private static final int UPTATE_INTERVAL_TIME = 70;// 傳感器管理器private SensorManager sensorManager;// 傳感器private Sensor sensor;// 重力感應(yīng)監(jiān)聽(tīng)器private OnShakeListenerCallBack onShakeListener;// 上下文private Context mContext;// 手機(jī)上一個(gè)位置時(shí)重力感應(yīng)坐標(biāo)private float lastX;private float lastY;private float lastZ;// 上次檢測(cè)時(shí)間private long lastUpdateTime;// 構(gòu)造器public ShakeListener(Context context) {// 獲得監(jiān)聽(tīng)對(duì)象mContext = context;start();}/**開(kāi)始重力傳感器的檢測(cè)*/public void start() {// 獲得傳感器管理器sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);if (sensorManager != null) {// 獲得重力傳感器sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);}// 注冊(cè)if (sensor != null) {sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);}}/**停止檢測(cè)*/public void stop() {sensorManager.unregisterListener(this);}/**重力感應(yīng)器感應(yīng)獲得變化數(shù)據(jù)*/@Overridepublic void onSensorChanged(SensorEvent event) {// 現(xiàn)在檢測(cè)時(shí)間long currentUpdateTime = System.currentTimeMillis();// 兩次檢測(cè)的時(shí)間間隔long timeInterval = currentUpdateTime - lastUpdateTime;// 判斷是否達(dá)到了檢測(cè)時(shí)間間隔if (timeInterval < UPTATE_INTERVAL_TIME)return;// 現(xiàn)在的時(shí)間變成last時(shí)間lastUpdateTime = currentUpdateTime;// 獲得x,y,z坐標(biāo)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;// 將現(xiàn)在的坐標(biāo)變成last坐標(biāo)lastX = x;lastY = y;lastZ = z;double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ) / timeInterval * 10000;// 達(dá)到速度閥值,發(fā)出提示if (speed >= SPEED_SHRESHOLD) {// ToastUtils.showToast(mContext,"速度"+speed);onShakeListener.onShake();}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}/**搖晃監(jiān)聽(tīng)接口*/public interface OnShakeListenerCallBack {public void onShake();}/**設(shè)置重力感應(yīng)監(jiān)聽(tīng)器*/public void setOnShakeListener(OnShakeListenerCallBack listener) {onShakeListener = listener;}}
Activity部分
public class BActivity extends AppCompatActivity implements ShakeAnimation.DoAnimationListener {@BindView(R.id.shake_flower)ImageView mShakeFlower;@BindView(R.id.shake_loading)LinearLayout mShakeLoading;@BindView(R.id.shake_result_img)ImageView mShakeResultImg;@BindView(R.id.shake_result_txt_name)TextView mShakeResultTxtName;@BindView(R.id.shake_result_txt_value)TextView mShakeResultTxtValue;@BindView(R.id.shake_result_layout)RelativeLayout mShakeResultLayout;@BindView(R.id.shakeImgUp_line)LinearLayout mShakeImgUpLine;@BindView(R.id.shakeImgUp)RelativeLayout mShakeImgUp;@BindView(R.id.shakeImgDown_line)LinearLayout mShakeImgDownLine;@BindView(R.id.shakeImgDown)RelativeLayout mShakeImgDown;private ShakeListener mShakeListener;private ShakeAnimation mShakeAnimation;private boolean hasReslut = false;private int shakeNum = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.shake_root);ButterKnife.bind(this);setResultVisible(false);//初始時(shí)不顯示結(jié)果showShakeLoading(false);//初始時(shí)不顯示加載initShake();}private void initShake() {Sound.getInstance().initPool(this);mShakeListener = new ShakeListener(this);mShakeListener.setOnShakeListener(new ShakeListener.OnShakeListenerCallBack() {@Overridepublic void onShake() {startShake();//開(kāi)始執(zhí)行動(dòng)畫mShakeListener.stop();//取消搖動(dòng)監(jiān)聽(tīng),動(dòng)畫結(jié)束之前不再監(jiān)聽(tīng)vibrate(500);Sound.getInstance().playStartSound();}});}/*** 執(zhí)行動(dòng)畫*/private void startShake() {mShakeAnimation = ShakeAnimation.getInstance(this);mShakeImgDown.startAnimation(mShakeAnimation.getDownAnim());mShakeImgUp.startAnimation(mShakeAnimation.getUpAnim());if (mShakeResultLayout != null && mShakeResultLayout.getVisibility() == View.VISIBLE) {showResultGoneAnim();}}/*** 搖出的結(jié)果顯示控制*/private void setResultVisible(boolean isVisible) {if (mShakeResultLayout == null) {return;}if (isVisible) {mShakeResultLayout.setVisibility(View.VISIBLE);} else {mShakeResultLayout.setVisibility(View.INVISIBLE);}}/*** 正在加載*/private void showShakeLoading(boolean isShow) {if (mShakeLoading == null) {return;}if (isShow) {mShakeLoading.setVisibility(View.VISIBLE);} else {mShakeLoading.setVisibility(View.GONE);}}private void setResultView(){showResultAnim();if(!hasReslut){mShakeResultImg.setImageResource(R.drawable.icon_sad_black);mShakeResultTxtName.setText("什么都沒(méi)搖到");mShakeResultTxtValue.setVisibility(View.GONE);}else {mShakeResultImg.setImageResource(R.drawable.a);mShakeResultTxtName.setText("渾水摸魚");mShakeResultTxtValue.setVisibility(View.VISIBLE);mShakeResultTxtValue.setText("距離10公里");}playResultSound();}private void playResultSound(){if(hasReslut){Sound.getInstance().playEndSound();}else {Sound.getInstance().playNotingSound();}shakeNum++;if(shakeNum%6==0){hasReslut = !hasReslut;}}/*** 動(dòng)畫開(kāi)啟*/@Overridepublic void onAnimStart(int code) {switch (code) {case ShakeAnimation.OPEN_ANIM_CODE:setHandLineVisible(true);break;}}/*** 動(dòng)畫結(jié)束*/@Overridepublic void onAnimEnd(int code) {switch (code) {case ShakeAnimation.COLSE_ANIM_CODE:setHandLineVisible(false);//setResultView();//showResultAnim();//顯示結(jié)果setResultVisible(true);break;case ShakeAnimation.SHAKE_RESULT_GONE_CODE:setResultVisible(false);break;case ShakeAnimation.SHAKE_RESULT_VISIBLE_CODE:mShakeListener.start();//動(dòng)畫結(jié)束了,重新開(kāi)始監(jiān)聽(tīng)搖動(dòng)break;}}/*** 手掌邊線顯示控制*/private void setHandLineVisible(boolean isVisible) {if (mShakeImgUpLine != null) {if (isVisible) {mShakeImgUpLine.setVisibility(View.VISIBLE);} else {mShakeImgUpLine.setVisibility(View.GONE);}}if (mShakeImgDownLine != null) {if (isVisible) {mShakeImgDownLine.setVisibility(View.VISIBLE);} else {mShakeImgDownLine.setVisibility(View.GONE);}}}/**顯示結(jié)果動(dòng)畫*/private void showResultAnim() {if (mShakeAnimation != null) {mShakeResultLayout.setAnimation(mShakeAnimation.getReusltAnim());}}/**顯示結(jié)果隱藏的動(dòng)畫*/private void showResultGoneAnim(){if (mShakeAnimation != null) {mShakeResultLayout.setAnimation(mShakeAnimation.getReusltGoneAnim());}}/**震動(dòng)*/private void vibrate(long milliseconds){Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);vibrator.vibrate(500);}@Overridepublic void onResume() {super.onResume();mShakeListener.start();}@Overridepublic void onPause() {mShakeListener.stop();super.onPause();}@Overridepublic void onStop() {mShakeListener.stop();super.onStop();}}
評(píng)論
圖片
表情
