Android實(shí)現(xiàn)微信搖一搖功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)微信搖一搖功能的具體代碼,供大家參考,具體內(nèi)容如下
1、初始化界面
設(shè)置搖一搖界面的背景圖片和搖動(dòng)時(shí)的上下兩半張圖片
<?xml version='1.0' encoding='utf-8'?><RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' android: android:layout_width='match_parent' android:layout_height='match_parent' tools:context='com.example.yyy.MainActivity' android:background='@mipmap/shakehideimg_man2' > <LinearLayout android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' > <ImageView android: android:layout_width='match_parent' android:layout_height='0dp' android:layout_weight='1' android:background='@mipmap/shake_logo_up' /> <ImageView android: android:layout_width='match_parent' android:layout_height='0dp' android:layout_weight='1' android:background='@mipmap/shake_logo_down' /> </LinearLayout></RelativeLayout>
2、Mainactivity - onCreate()
private ImageView imgDown; private ImageView imgUp; private SensorManager sensorManager; private SensorEventListener sensorEventListener; private Sensor accSensor; private AnimationSet upAnimationSet; private AnimationSet downAnimationSet; private SoundPool soundPool; private int soundId; private Vibrator vibrator; private boolean isYYY = false; /*1.初始化頁面 2.初始化數(shù)據(jù) * 3.監(jiān)聽加速度變化(觸發(fā)條件) * 1.圖片執(zhí)行動(dòng)畫 * ***2.到服務(wù)器查詢同一時(shí)間搖一搖的異性用戶 * 2.播放音樂 * 3.振動(dòng) * **4.當(dāng)你正在搖的時(shí)候(不能再搖動(dòng)) * */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); initEvent(); //注冊(cè)監(jiān)聽 sensorManager.registerListener(sensorEventListener,accSensor,SENSOR_DELAY_NORMAL);}
3、初始化數(shù)據(jù)
private void initData() { //先獲得傳感器管理器 sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); //獲得加速度傳感器 accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //獲得振動(dòng)器 vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); //初始化聲音池 soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); //初始化聲音資源 soundId = soundPool.load(this,R.raw.awe,1); //初始化動(dòng)畫 upAnimationSet = new AnimationSet(true); TranslateAnimation upUpAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -0.5f); upUpAnimation.setDuration(500); TranslateAnimation upDownAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0); upDownAnimation.setDuration(500); //down動(dòng)畫在up動(dòng)畫之后執(zhí)行 upUpAnimation.setStartOffset(500); upAnimationSet.addAnimation(upUpAnimation); upAnimationSet.addAnimation(upDownAnimation); upAnimationSet.setDuration(1000); upAnimationSet.setStartOffset(200); //初始化動(dòng)畫 downAnimationSet = new AnimationSet(true); TranslateAnimation downUpAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0); downUpAnimation.setDuration(500); TranslateAnimation downDownAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f); downDownAnimation.setDuration(500); //down動(dòng)畫在up動(dòng)畫之后執(zhí)行 downDownAnimation.setStartOffset(500); downAnimationSet.addAnimation(downDownAnimation); downAnimationSet.addAnimation(downUpAnimation); downAnimationSet.setDuration(1000); downAnimationSet.setStartOffset(200); }
4、初始化事件 - 搖一搖
給加速度感應(yīng)器設(shè)置監(jiān)聽① 設(shè)置搖一搖的觸發(fā)條件② 播放動(dòng)畫③ 播放音樂④ 開啟震動(dòng)
private void initEvent() { sensorEventListener = new SensorEventListener() { /* * 當(dāng)傳感器的值發(fā)生變化時(shí)的回調(diào) * */ @Override public void onSensorChanged(SensorEvent event) { //Log.i('AAA', 'onSensorChanged: '); //設(shè)置觸發(fā)搖一搖的條件 //獲得x,y,z方向的變化 float[] values = event.values; float valueX = values[0]; //空間中X的變化 float valueY = values[1]; //空間中Y的變化 float valueZ = values[2]; //空間中Z的變化 if(valueX > 15 || valueY > 15 || valueZ >15){//觸發(fā)條件 if(!isYYY){ imgUp.startAnimation(upAnimationSet); imgDown.startAnimation(downAnimationSet); //播放音樂 soundPool.play(soundId,1,1,1,0,1); //振動(dòng) vibrator.vibrate(new long[]{200,400,200,400,200,400,200,400},-1); } } } /* *當(dāng)傳感器精度發(fā)生變化的回調(diào) * */ @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; upAnimationSet.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { isYYY = true; } @Override public void onAnimationEnd(Animation animation) { isYYY = false; } @Override public void onAnimationRepeat(Animation animation) { } });}
5、添加權(quán)限
<uses-permission android:name='android.permission.VIBRATE'></uses-permission>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
