Activity
package bw.com.bw_day09.demo04;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import bw.com.bw_day09.R;
public class SensorActivity04 extends AppCompatActivity implements SensorEventListener{
private ImageView mShakeUp;
private ImageView mShakeDown;
//定义动画
private TranslateAnimation animationUp;
private TranslateAnimation animationDown;
private SensorManager sensorManager;
//todo 添加震动 -- 添加权限
private Vibrator vibrator;
//TODO 添加声音
private SoundPool soundPool;//声音池
private int mLoadId;//当前播放的声音
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor04);
initView();
initAnimation();//初始化动画
//TODO 1, 得到传感器管理器对象
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//todo 添加震动
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
//TODO 添加声音
//最大数量, 类型, 质量 默认0
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
mLoadId = soundPool.load(this,R.raw.awe,1);
}
//TODO 2, 注册传感器
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,sensorManager.getDefaultSensor(1),SensorManager.SENSOR_DELAY_NORMAL);
}
//TODO 3, 取消注册传感器
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
private void initView() {
mShakeUp = (ImageView) findViewById(R.id.shake_up_id);
mShakeDown = (ImageView) findViewById(R.id.shake_down_id);
}
private void initAnimation()
{
//补间动画 --- 位移
animationUp = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,-1);
animationUp.setDuration(3000);
animationDown = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,1);
animationDown.setDuration(3000);
}
//----监听器的回调方法---
@Override
public void onSensorChanged(SensorEvent event) {
//TODO 4, 获取数据, 晃动手机, 播放动画
float[] data = event.values;
if(Math.abs(data[0])>13 || Math.abs(data[1])>13 || Math.abs(data[2])>13 )
{
//TODO 添加震动
long[] pattern = {300,500};
vibrator.vibrate(pattern,-1);
//TODO 添加声音
//声音id, 左声道(0.0-1.0),右声道,优先级, 是否循环播放(0 不循环,-1 循环), 播放的比例(1 正常比例)
soundPool.play(mLoadId,1.0f,1.0f,1,0,1.0f);
//TODO 5, 启动动画
mShakeUp.startAnimation(animationUp);
mShakeDown.startAnimation(animationDown);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
添加震动权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bw.com.bw_day09">
<!-- 添加震动权限 -->
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".demo01.SensorActivity01" />
<activity android:name=".demo02.SensorActivity02" />
<activity android:name=".demo03.SensorActivity03" />
<activity android:name=".demo04.SensorActivity04"></activity>
</application>
</manifest>
布局
<?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"
tools:context="bw.com.bw_day09.demo04.SensorActivity04">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/shakehideimg_man2"
android:layout_centerInParent="true"
android:id="@+id/flower_id"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/shake_logo_up"
android:id="@+id/shake_up_id"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/shake_logo_down"
android:id="@+id/shake_down_id"
/>
</LinearLayout>
</RelativeLayout>