Android——使用加速度传感器实现摇一摇功能(带音效、动画效果)

最近项目中有个摇一摇功能,于是写个Demo,以作分享,顺便复习下传感器方面的应用。

1,main代码:
     
     
     
     
public class MainActivity extends Activity implements SensorEventListener {
private final int DURATION_TIME = 600;// 值越大,摇晃的力度越大
private Vibrator mVibrator;
SensorManager mSensorManager;
private RelativeLayout mImgUp;
private RelativeLayout mImgDn;
private SoundPool mSndPool;
private HashMap<Integer, Integer> mSoundPoolMap = new HashMap<Integer, Integer>();
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
mImgUp = (RelativeLayout) findViewById(R.id.shakeImgUp);
mImgDn = (RelativeLayout) findViewById(R.id.shakeImgDown);
// 获取传感器管理服务
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// 获取震动服务
mVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
// 获取加速度传感器服务
Sensor mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// 注册监听,获取传感器的变化值
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_UI);
// 加载音效
loadSound();
}
 
@Override
public void onSensorChanged(SensorEvent event) {
int sensorType = event.sensor.getType();
float[] val = event.values;
if (sensorType == Sensor.TYPE_ACCELEROMETER) {
if ((Math.abs(val[0]) > 17 || Math.abs(val[1]) > 17 || Math.abs(val[2]) > 17)) {
mVibrator.vibrate(500);// 震动
startAnim();// 执行动画
}
}
}
 
// 加载音效
@SuppressWarnings("deprecation")
private void loadSound() {
mSndPool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 5);
new Thread() {
public void run() {
mSoundPoolMap.put(0, mSndPool.load(MainActivity.this, R.raw.shake_sound_male, 1));
mSoundPoolMap.put(1, mSndPool.load(MainActivity.this, R.raw.shake_match, 1));
}
}.start();
}
 
// 开启动画
public void startAnim() {
AnimationSet animup = new AnimationSet(true);
TranslateAnimation mytranslateanimup0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
-0.5f);
mytranslateanimup0.setDuration(DURATION_TIME);
TranslateAnimation mytranslateanimup1 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
+0.5f);
mytranslateanimup1.setDuration(DURATION_TIME);
mytranslateanimup1.setStartOffset(DURATION_TIME);
animup.addAnimation(mytranslateanimup0);
animup.addAnimation(mytranslateanimup1);
mImgUp.startAnimation(animup);
 
AnimationSet animdn = new AnimationSet(true);
TranslateAnimation mytranslateanimdn0 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
+0.5f);
mytranslateanimdn0.setDuration(DURATION_TIME);
TranslateAnimation mytranslateanimdn1 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
-0.5f);
mytranslateanimdn1.setDuration(DURATION_TIME);
mytranslateanimdn1.setStartOffset(DURATION_TIME);
animdn.addAnimation(mytranslateanimdn0);
animdn.addAnimation(mytranslateanimdn1);
mImgDn.startAnimation(animdn);
 
// 动画监听,开始时显示加载状态,
mytranslateanimdn0.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// 播放音效
mSndPool.play(mSoundPoolMap.get(0), (float) 0.2, (float) 0.2, 0, 0, (float) 0.6);
}
 
@Override
public void onAnimationRepeat(Animation animation) {
 
}
 
@Override
public void onAnimationEnd(Animation animation) {
mSndPool.play(mSoundPoolMap.get(1), (float) 0.2, (float) 0.2, 0,0, (float) 0.6);
Toast.makeText(getBaseContext(), "摇一摇结束", Toast.LENGTH_SHORT).show();
}
});
}
 
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
 
}
 
@Override
protected void onDestroy() {
super.onDestroy();
// 取消注册监听
mSensorManager.unregisterListener(this);
}
}

2,布局:
     
     
     
     
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#111"
android:orientation="vertical" >
 
<ImageView
android:id="@+id/shakeBg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="@null"
android:src="@drawable/shakehideimg_man" />
 
<LinearLayout
android:id="@+id/shake_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical" >
 
<RelativeLayout
android:id="@+id/shakeImgUp"
android:layout_width="fill_parent"
android:layout_height="180dip"
android:background="#111"
android:visibility="visible" >
 
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:contentDescription="@null"
android:src="@drawable/arq" />
</RelativeLayout>
 
<RelativeLayout
android:id="@+id/shakeImgDown"
android:layout_width="fill_parent"
android:layout_height="180dip"
android:background="#111"
android:visibility="visible" >
 
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:contentDescription="@null"
android:src="@drawable/arn" />
</RelativeLayout>
</LinearLayout>
 
</RelativeLayout>
    
源码下载: http://download.csdn.net/detail/airsaid/9447308

你可能感兴趣的:(Android——使用加速度传感器实现摇一摇功能(带音效、动画效果))