Android實現(xiàn)微信搖一搖功能
本文實例為大家分享了Android實現(xiàn)微信搖一搖功能的具體代碼,供大家參考,具體內(nèi)容如下
1、初始化界面
設(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:id="@+id/activity_main" 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:id="@+id/img_up" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@mipmap/shake_logo_up" /> <ImageView android:id="@+id/img_down" 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í)行動畫 * ***2.到服務(wù)器查詢同一時間搖一搖的異性用戶 * 2.播放音樂 * 3.振動 * **4.當(dāng)你正在搖的時候(不能再搖動) * */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); initEvent(); //注冊監(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); //獲得振動器 vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); //初始化聲音池 soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); //初始化聲音資源 soundId = soundPool.load(this,R.raw.awe,1); //初始化動畫 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動畫在up動畫之后執(zhí)行 upUpAnimation.setStartOffset(500); upAnimationSet.addAnimation(upUpAnimation); upAnimationSet.addAnimation(upDownAnimation); upAnimationSet.setDuration(1000); upAnimationSet.setStartOffset(200); //初始化動畫 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動畫在up動畫之后執(zhí)行 downDownAnimation.setStartOffset(500); downAnimationSet.addAnimation(downDownAnimation); downAnimationSet.addAnimation(downUpAnimation); downAnimationSet.setDuration(1000); downAnimationSet.setStartOffset(200); }
4、初始化事件 - 搖一搖
給加速度感應(yīng)器設(shè)置監(jiān)聽
① 設(shè)置搖一搖的觸發(fā)條件
② 播放動畫
③ 播放音樂
④ 開啟震動
private void initEvent() { sensorEventListener = new SensorEventListener() { /* * 當(dāng)傳感器的值發(fā)生變化時的回調(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); //振動 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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react native打包apk文件安裝好之后進入應(yīng)用閃退的解決方案
這篇文章主要介紹了react native打包apk文件安裝好之后進入應(yīng)用閃退的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09詳解Android應(yīng)用開發(fā)--MP3音樂播放器代碼實現(xiàn)(一)
這篇文章主要介紹了詳解Android應(yīng)用開發(fā)--MP3音樂播放器代碼實現(xiàn)(一),非常具有實用價值,需要的朋友可以參考下 。2017-01-01Android Studio升級3.6 Build窗口出現(xiàn)中文亂碼問題解決方法
這篇文章主要介紹了Android Studio升級3.6 Build窗口出現(xiàn)中文亂碼問題解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Android創(chuàng)建與解析XML(二)——詳解Dom方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(二)——詳解Dom方式 ,這里整理了詳細的代碼,有需要的小伙伴可以參考下。2016-11-11一個簡單的toolabar結(jié)合drawlayout使用方法
這篇文章主要為大家詳細介紹了一個簡單的toolabar結(jié)合drawlayout的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android 使用mediaplayer播放res/raw文件夾中的音樂的實例
這篇文章主要介紹了Android 使用mediaplayer播放res/raw文件夾中的音樂的實例的相關(guān)資料,需要的朋友可以參考下2017-04-04Android動畫 實現(xiàn)開關(guān)按鈕動畫(屬性動畫之平移動畫)實例代碼
這篇文章主要介紹了Android動畫 實現(xiàn)開關(guān)按鈕動畫(屬性動畫之平移動畫)實例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11Android中TabLayout+ViewPager 簡單實現(xiàn)app底部Tab導(dǎo)航欄
TabLayout 是Android com.android.support:design庫的一個控件。本文主要給大家介紹TabLayout+ViewPager 簡單實現(xiàn)app底部Tab布局,需要的的朋友參考下2017-02-02