标签:
Android系统带的传感器有很多种,最常见的莫过于微信的摇一摇了,那么今天我们就来看看Anroid中传感器的使用,做一个类似于微信摇一摇的效果。
OK ,废话不多说,我们就先来看看效果图吧:
当我摇动手机的时候这里的动画效果基本和微信上的动画效果一致,这里请大家自行脑补微信摇一摇画面。
那我们就动手吧。
好,那我们先来看看布局文件吧,在布局文件的正中央是一个花的图片,上图大家看到的手机图片实际上是两张图片拼接在一起,将花的那张图片遮住了,当摇一摇的时候,这两张图片分别向上或者向下移动,然后花的图片就可以显示出来。OK,基本原理就是这样,我们来看看代码:
<?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:layout_width="match_parent" android:layout_height="match_parent" android:background="#1f1f1f"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/flower"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical"> <ImageView android:id="@+id/up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/up"/> <ImageView android:id="@+id/down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/down"/> </LinearLayout> </RelativeLayout>
既然要监听手机加速度的变化,那我首先需要获取系统的传感器:
//获取到一个传感器管理器 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); //获得一个加速度传感器 Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
1. SENSOR_DELAY_FASTEST 2. SENSOR_DELAY_GAME 3. SENSOR_DELAY_UI 4. SENSOR_DELAY_NORMAL
OK ,注册完之后,我们还是来看看这个监听器是什么吧:
private SensorEventListener listener = new SensorEventListener() { //当手机的加速度发生变化时调用 @Override public void onSensorChanged(SensorEvent event) { //获取手机在不同方向上加速度的变化 float valuesX = Math.abs(event.values[0]); float valuesY = Math.abs(event.values[1]); float valuesZ = Math.abs(event.values[2]); if (valuesX > 17 || valuesY > 17 || valuesZ > 17) { startAnimation(); playSound(); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } };
动画实际上就是两个平移动画,我们来看看:
private void startAnimation() { //如果两次晃动手机的时间小于1秒,则只执行一次动画 long currentTimeMillis = System.currentTimeMillis(); if (currentTimeMillis - lastTime < 1000) { return; } lastTime = currentTimeMillis; AnimationSet upSet = new AnimationSet(true); TranslateAnimation upUp = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1); upUp.setDuration(1000); TranslateAnimation upDown = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1); upDown.setDuration(1000); upDown.setStartOffset(1000); upSet.addAnimation(upUp); upSet.addAnimation(upDown); up.startAnimation(upSet); AnimationSet downSet = new AnimationSet(true); TranslateAnimation downUp = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1); downUp.setDuration(1000); TranslateAnimation downDown = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1); downDown.setDuration(1000); downDown.setStartOffset(1000); downSet.addAnimation(downUp); downSet.addAnimation(downDown); down.startAnimation(downSet); }
/** * 初始化声音池 */ private void initSoundPool() { if (Build.VERSION.SDK_INT > 20) { SoundPool.Builder builder = new SoundPool.Builder(); //1.最大并发流数 builder.setMaxStreams(3); AudioAttributes.Builder aaBuilder = new AudioAttributes.Builder(); aaBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC); builder.setAudioAttributes(aaBuilder.build()); soundPool = builder.build(); } else { soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0); } //加载一个音频文件 sound1 = soundPool.load(this, R.raw.awe, 1); }
//1.声音的id //2.3.表示左右声道的音量 //4.优先级 //5.是否循环 //6.声音播放速率 soundPool.play(sound1, 1, 1, 0, 0, 1);
最后一步就是开启手机震动了,开启手机震动,我需要首先获取震动服务,如下:
//获取手机震动服务 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
//1.表示震动的节奏off/on/off/on/off/on...... //2.表示是否重复震动,-1表示不重复 vibrator.vibrate(new long[]{100, 200, 100, 200, 100, 200}, -1);
<uses-permission android:name="android.permission.VIBRATE" />
@Override protected void onDestroy() { super.onDestroy(); //解除对加速度传感器的监听 sensorManager.unregisterListener(listener); if (soundPool != null) { //声音池释放资源 soundPool.release(); } }
public class MainActivity extends AppCompatActivity { private ImageView up; private ImageView down; //上一次晃动手机的时间 private long lastTime; private SoundPool soundPool; private int sound1; private Vibrator vibrator; private SensorEventListener listener = new SensorEventListener() { //当手机的加速度发生变化时调用 @Override public void onSensorChanged(SensorEvent event) { //获取手机在不同方向上加速度的变化 float valuesX = Math.abs(event.values[0]); float valuesY = Math.abs(event.values[1]); float valuesZ = Math.abs(event.values[2]); if (valuesX > 17 || valuesY > 17 || valuesZ > 17) { startAnimation(); playSound(); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; private SensorManager sensorManager; private void playSound() { //1.声音的id //2.3.表示左右声道的音量 //4.优先级 //5.是否循环 //6.声音播放速率 soundPool.play(sound1, 1, 1, 0, 0, 1); //手机震动 //1.表示震动的节奏off/on/off/on/off/on...... //2.表示是否重复震动,-1表示不重复 vibrator.vibrate(new long[]{100, 200, 100, 200, 100, 200}, -1); } private void startAnimation() { //如果两次晃动手机的时间小于1秒,则只执行一次动画 long currentTimeMillis = System.currentTimeMillis(); if (currentTimeMillis - lastTime < 1000) { return; } lastTime = currentTimeMillis; AnimationSet upSet = new AnimationSet(true); TranslateAnimation upUp = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1); upUp.setDuration(1000); TranslateAnimation upDown = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1); upDown.setDuration(1000); upDown.setStartOffset(1000); upSet.addAnimation(upUp); upSet.addAnimation(upDown); up.startAnimation(upSet); AnimationSet downSet = new AnimationSet(true); TranslateAnimation downUp = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1); downUp.setDuration(1000); TranslateAnimation downDown = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1); downDown.setDuration(1000); downDown.setStartOffset(1000); downSet.addAnimation(downUp); downSet.addAnimation(downDown); down.startAnimation(downSet); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); up = ((ImageView) findViewById(R.id.up)); down = ((ImageView) findViewById(R.id.down)); initSensor(); initSoundPool(); //获取手机震动服务 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); } /** * 初始化声音池 */ private void initSoundPool() { if (Build.VERSION.SDK_INT > 20) { SoundPool.Builder builder = new SoundPool.Builder(); //1.最大并发流数 builder.setMaxStreams(3); AudioAttributes.Builder aaBuilder = new AudioAttributes.Builder(); aaBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC); builder.setAudioAttributes(aaBuilder.build()); soundPool = builder.build(); } else { soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0); } //加载一个音频文件 sound1 = soundPool.load(this, R.raw.awe, 1); } /** * 初始化传感器 */ private void initSensor() { //获取到一个传感器管理器 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); //获得一个加速度传感器 Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //注册传感器监听, //1.监听器 //2.加速度传感器 //3.传感器灵敏度 //传感器灵敏度分为四级,从上往下灵敏度依次降低 //SENSOR_DELAY_FASTEST //SENSOR_DELAY_GAME //SENSOR_DELAY_UI //SENSOR_DELAY_NORMAL sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME); } @Override protected void onDestroy() { super.onDestroy(); //解除对加速度传感器的监听 sensorManager.unregisterListener(listener); if (soundPool != null) { //声音池释放资源 soundPool.release(); } } }
标签:
原文地址:http://blog.csdn.net/u012702547/article/details/51339616