Android实现微信摇一摇功能

家电修理 2023-07-16 19:17www.caominkang.com电器维修

本文实例为大家讲解了Android实现微信摇一摇功能的具体代码,供大家参考,具体内容如下

1、初始化界面

设置摇一摇界面的背景图片和摇动时的上下两半张图片



 
 
 

 


2、Mainactivity - onCreate()

private ImageVie imgDon;
 private ImageVie imgUp;
 private SensorManager sensorManager;
 private SensorEventListener sensorEventListener;
 private Sensor aSensor;
 private AnimationSet upAnimationSet;
 private AnimationSet donAnimationSet;
 private SoundPool soundPool;
 private int soundId;
 private Vibrator vibrator;

 private boolean isYYY = false;

 


 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentVie(R.layout.activity_main);
  initVie();
  initData();
  initEvent();
  //注册监听
  sensorManager.registerListener(sensorEventListener,aSensor,SENSOR_DELAY_NORMAL);

}

3、初始化数据

private void initData() {
  //先获得传感器管理器
  sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  //获得加速度传感器
  aSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  //获得振动器
  vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

  //初始化声音池
  soundPool = ne SoundPool(4, AudioManager.STREAM_MUSIC, 0);

  //初始化声音资源
  soundId = soundPool.load(this,R.ra.ae,1);


  //初始化动画
  upAnimationSet = ne AnimationSet(true);
  TranslateAnimation upUpAnimation = ne 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 upDonAnimation = ne TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
 Animation.RELATIVE_TO_SELF, 0,
 Animation.RELATIVE_TO_SELF, -0.5f,
 Animation.RELATIVE_TO_SELF, 0);
  upDonAnimation.setDuration(500);
  //don动画在up动画之后执行
  upUpAnimation.setStartOffset(500);

  upAnimationSet.addAnimation(upUpAnimation);
  upAnimationSet.addAnimation(upDonAnimation);

  upAnimationSet.setDuration(1000);
  upAnimationSet.setStartOffset(200);


  //初始化动画
  donAnimationSet = ne AnimationSet(true);
  TranslateAnimation donUpAnimation = ne TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
 Animation.RELATIVE_TO_SELF, 0,
 Animation.RELATIVE_TO_SELF, 0.5f,
 Animation.RELATIVE_TO_SELF, 0);
  donUpAnimation.setDuration(500);
  TranslateAnimation donDonAnimation = ne TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
 Animation.RELATIVE_TO_SELF, 0,
 Animation.RELATIVE_TO_SELF, 0,
 Animation.RELATIVE_TO_SELF, 0.5f);
  donDonAnimation.setDuration(500);
  //don动画在up动画之后执行
  donDonAnimation.setStartOffset(500);

  donAnimationSet.addAnimation(donDonAnimation);
  donAnimationSet.addAnimation(donUpAnimation);

  donAnimationSet.setDuration(1000);
  donAnimationSet.setStartOffset(200);

 }

4、初始化事件 - 摇一摇

给加速度感应器设置监听
① 设置摇一摇的触发条件
② 播放动画
③ 播放音乐
④ 开启震动

private void initEvent() {
  sensorEventListener = ne SensorEventListener() {
   
   @Override
   public void onSensorChanged(SensorEvent event) {
 //Log.i("AAA", "onSensorChanged: ");
  //设置触发摇一摇的条件
  //获得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){//触发条件
   if(!isYYY){
imgUp.startAnimation(upAnimationSet);
imgDon.startAnimation(donAnimationSet);
//播放音乐
soundPool.play(soundId,1,1,1,0,1);
//振动
vibrator.vibrate(ne long[]{200,400,200,400,200,400,200,400},-1);
   }

  }


   }

   
   @Override
   public void onAuracyChanged(Sensor sensor, int auracy) {

   }
  };

  upAnimationSet.setAnimationListener(ne 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、添加权限

Copyright © 2016-2025 www.caominkang.com 曹敏电脑维修网 版权所有 Power by