亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Android實現微信搖一搖功能

瀏覽:80日期:2022-06-06 10:37:14

本文實例為大家分享了Android實現微信搖一搖功能的具體代碼,供大家參考,具體內容如下

1、初始化界面

設置搖一搖界面的背景圖片和搖動時的上下兩半張圖片

Android實現微信搖一搖功能

<?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.初始化數據 * 3.監(jiān)聽加速度變化(觸發(fā)條件) * 1.圖片執(zhí)行動畫 * ***2.到服務器查詢同一時間搖一搖的異性用戶 * 2.播放音樂 * 3.振動 * **4.當你正在搖的時候(不能再搖動) * */ @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、初始化數據

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、初始化事件 - 搖一搖

給加速度感應器設置監(jiān)聽① 設置搖一搖的觸發(fā)條件② 播放動畫③ 播放音樂④ 開啟震動

private void initEvent() { sensorEventListener = new SensorEventListener() { /* * 當傳感器的值發(fā)生變化時的回調 * */ @Override public void onSensorChanged(SensorEvent event) { //Log.i('AAA', 'onSensorChanged: '); //設置觸發(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); } } } /* *當傳感器精度發(fā)生變化的回調 * */ @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、添加權限

<uses-permission android:name='android.permission.VIBRATE'></uses-permission>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: 微信
相關文章:
主站蜘蛛池模板: 亚洲成人一区二区 | 韩国免费人成在线观看网站 | 国内一级特黄女人精品片 | 青青操网| 久久精品在 | 久久人体做爰大胆 | 日韩欧美a级高清毛片 | 日本大片免a费观看视频+播放器 | 黄色毛片看看 | 九九精品影院 | 正在播放一区 | 欧美成亚洲 | 国产精品第五页 | 免费大片黄在线观看日本 | 亚洲免费网 | 婷婷综合国产激情在线 | 久久高清一区二区三区 | 国产初高中生厕所小便 | 正在播放国产无套露脸 | 中国成熟xxx视频 | 中文字幕一区二区精品区 | 草逼网站| 日韩亚洲欧美一区噜噜噜 | 国产刚刚发育被强j在线播放 | 97超级碰久久久久香蕉人人 | 国产精品美女久久久久网站 | 国产视频首页 | 免费人成网ww44kk44 | 91网址在线 | 亚洲欧美一区二区三区不卡 | 欧美午夜a级精美理论片 | 91亚洲一区二区在线观看不卡 | 91免费精品国偷自产在线在线 | 妖精视频在线观看网站 | 欧美亚洲国产一区二区三区 | 欧美 综合 社区 国产 | 加勒比一道本综合 | 国产视频黄 | 亚洲色图在线观看视频 | 国产大片免费在线观看 | 国产叼嘿久久精品久久 |