欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现微信摇一摇功能

程序员文章站 2022-07-09 17:15:16
本文实例为大家分享了android实现微信摇一摇功能的具体代码,供大家参考,具体内容如下1、初始化界面设置摇一摇界面的背景图片和摇动时的上下两半张图片

本文实例为大家分享了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: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.初始化数据
 * 3.监听加速度变化(触发条件)
 *  1.图片执行动画
 *  ***2.到服务器查询同一时间摇一摇的异性用户
 *  2.播放音乐
 *  3.振动
 *  **4.当你正在摇的时候(不能再摇动)
 * */


 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  initview();
  initdata();
  initevent();
  //注册监听
  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动画之后执行
  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动画之后执行
  downdownanimation.setstartoffset(500);

  downanimationset.addanimation(downdownanimation);
  downanimationset.addanimation(downupanimation);

  downanimationset.setduration(1000);
  downanimationset.setstartoffset(200);

 }

4、初始化事件 - 摇一摇

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

private void initevent() {
  sensoreventlistener = new 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);
       imgdown.startanimation(downanimationset);
       //播放音乐
       soundpool.play(soundid,1,1,1,0,1);
       //振动
       vibrator.vibrate(new long[]{200,400,200,400,200,400,200,400},-1);
      }

     }


   }

   /*
   *当传感器精度发生变化的回调
   * */
   @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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。