溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用Android怎么實現(xiàn)一個微信搖一搖功能

發(fā)布時間:2021-01-27 15:29:17 來源:億速云 閱讀:277 作者:Leah 欄目:開發(fā)技術

這期內(nèi)容當中小編將會給大家?guī)碛嘘P利用Android怎么實現(xiàn)一個微信搖一搖功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

1、初始化界面

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

利用Android怎么實現(xiàn)一個微信搖一搖功能

<?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.到服務器查詢同一時間搖一搖的異性用戶
 *  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、初始化數(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、初始化事件 - 搖一搖

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

private void initEvent() {
  sensorEventListener = new SensorEventListener() {
   /*
   * 當傳感器的值發(fā)生變化時的回調(diào)
   * */
   @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ā)生變化的回調(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、添加權限

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

上述就是小編為大家分享的利用Android怎么實現(xiàn)一個微信搖一搖功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI