在上一篇博客当中提到了传感器的分类,基本信息和常见的使用方式。在本篇博客当中会对于上篇博客的传感器的使用方法做出进一步的挖掘,完成微信当中的摇一摇功能。
思路分析:
手机摇晃从而发生图片位置的改变,说明要检测手机的晃动,根据上节课的学习分析,可以通过加速度传感器判断手机的摇晃,图片位置发生改变,然后还会重新回到原来的位置,应该对于图片设置动画。摇一摇当中还包括震动效果和音效,那么我们可以使用震动管理器添加手机震动效果,使用音效池增加背景音乐。
分析了具体实现的功能之后,我们开始按照这些思路,编写代码:
1.绘制布局:activity_shake.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<ImageView
android:id="@+id/img_flower"
android:src="@mipmap/shakehideimg_man2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<LinearLayout
android:id="@+id/img_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/img_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/shake_logo_up"/>
<ImageView
android:id="@+id/img_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/shake_logo_down"/>
</LinearLayout>
</RelativeLayout>
2.在activity当中编写代码
public class ShakeActivity extends AppCompatActivity {
private ImageView iv_flower;
private ImageView iv_up;
private ImageView iv_down;
// 声明动画对象
private TranslateAnimation upAnimation ,downAnimation;
// 声明振动器对象
private Vibrator vibrator;
private SensorManager manager;
private Sensor sensor;
// 声明放置背景音乐的声音池对象
private SoundPool soundPool;
private int load;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shake);
iv_flower = (ImageView) findViewById(R.id.img_flower);
iv_up = (ImageView) findViewById(R.id.img_up);
iv_down = (ImageView) findViewById(R.id.img_down);
// 1.获取传感器管理者对象
manager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
//2. 获取传感器对象
sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
vibrator = (Vibrator)this.getSystemService(Context.VIBRATOR_SERVICE);
soundPool = new SoundPool(6,AudioManager.STREAM_MUSIC,0);
// 向声音池当中加载音乐的过程
load = soundPool.load(this, R.raw.awe, 1);
initAnimation();
}
SensorEventListener listener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
int coll = 18;
if (Math.abs(values[0])>18|Math.abs(values[1])>18|Math.abs(values[2])>18){
//开始震动,播放背景音乐,同时播放动画
long pattern[] = {200,400};
vibrator.vibrate(pattern,-1);
// 播放背景音乐
soundPool.play(load,1.0f,1.0f,1,0,1.0f);
// 开启动画
iv_up.startAnimation(upAnimation);
iv_down.startAnimation(downAnimation);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
@Override
protected void onResume() {
super.onResume();
// 注册监听器
manager.registerListener(listener,sensor,manager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
manager.unregisterListener(listener);//注销传感器的监听器
}
//声明动画对象
public void initAnimation(){
upAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1);
upAnimation.setRepeatCount(1);
upAnimation.setRepeatMode(Animation.REVERSE);
upAnimation.setDuration(2000); //上面图片的动画的设置
downAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
downAnimation.setRepeatMode(Animation.REVERSE);
downAnimation.setRepeatCount(1);
downAnimation.setDuration(2000);//下面图片的动画的设置
}
}
最后要注意的依然是添加手机震动权限
<uses-permission android:name="android.permission.VIBRATE" />