/**
* 摇一摇:仿微信效果
* 实现步骤:借助加速度传感器,根据加速度传感器设置事件,可以获得传感器返回的值
* 写一个逻辑,每隔轴上检测加速度都大于5,判断摇晃了手机(震动,添加动画)
*/
public class ShakeActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager sensorManager;
private Sensor accelerrometer;
Vibrator vibrator;
private ImageView imgDown;
private ImageView imgUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shake);
imgDown = (ImageView) findViewById(R.id.img_down);
imgUp = (ImageView) findViewById(R.id.img_up);
//获得传感器管理器
sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
//获得震动管理的系统服务
vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
//通过传感器获得具体的加速度传感器
accelerrometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
@Override
protected void onResume() {
super.onResume();
//1.传感器的监听Listener 2.监听的具体传感器 3.监听延迟率(对应4中模式)
//SensorManager.SENSOR_DELAY_UI 更新UI
//SensorManager.SENSOR_DELAY_GAME 一般游戏中使用
//SensorManager.SENSOR_DELAY_NORMAL 一般
//SensorManager.SENSOR_DELAY_FASTEST 反应最快
sensorManager.registerListener(this, accelerrometer, SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void onDestroy() {
super.onDestroy();
//一旦注册必须要进行解绑操作
sensorManager.unregisterListener(this);
}
//
//监听传感器事件的返回方法
//监听到到值返回在SensorEvent float []
@Override
public void onSensorChanged(SensorEvent event) {
//TODO 获得值到处理逻辑 根据项目需求来定
float[] values = event.values;
if (values[0] > 5 && values[1] > 5 && values[2] > 5) {
//设置震动时间
vibrator.vibrate(1000);
Toast.makeText(this, "摇晃了", Toast.LENGTH_SHORT).show();
startAnimation();
}
}
private void startAnimation() {
//开启动画效果
//2张图片 上面那张图片:向上移动,向下移动 下面这张图片:向下移动,向上移动
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation upImageup = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1
);
//设置动画时间
upImageup.setDuration(1000);
animationSet.addAnimation(upImageup);
TranslateAnimation upImagedown = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1
);
//第一个动画执行完毕,在开启第二个动画
upImagedown.setStartOffset(1000);
//设置动画时间
upImagedown.setDuration(1000);
animationSet.addAnimation(upImagedown);
//作用在上面那张图片上面
imgUp.startAnimation(animationSet);
AnimationSet animationSetTwo = new AnimationSet(true);
TranslateAnimation downImagedown = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1
);
//设置动画时间
downImagedown.setDuration(1000);
animationSetTwo.addAnimation(downImagedown);
TranslateAnimation dwonImageup = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1
);
//第一个动画执行完毕,在开启第二个动画
dwonImageup.setStartOffset(1000);
//设置动画时间
dwonImageup.setDuration(1000);
animationSetTwo.addAnimation(dwonImageup);
imgDown.startAnimation(animationSetTwo);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}