android 微信摇一摇代码,Android实现微信摇一摇功能

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

1、初始化界面

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

426c443e2b2983cf78b699c31724994d.png

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"

>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

android:id="@+id/img_up"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:background="@mipmap/shake_logo_up"

/>

android:id="@+id/img_down"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:background="@mipmap/shake_logo_down"

/>

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、添加权限

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值