欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用SensorEventListener改写的Shake类

程序员文章站 2022-04-20 08:29:32
...
抄了别人的一段代码,发现使用了SensorListener,在1.5上会有警告,于是改成SensorEventListener了。
摇6下就会触发onShake()事件了。


package com.test.android.shake;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class ShakeListener implements SensorEventListener {

private static final int FORCE_THRESHOLD = 350;
private static final int TIME_THRESHOLD = 100;
private static final int SHAKE_TIMEOUT = 500;
private static final int SHAKE_DURATION = 1000;
private static final int SHAKE_COUNT = 6;

private SensorManager mSensorMgr;
private float mLastX = -1.0f, mLastY = -1.0f, mLastZ = -1.0f;
private long mLastTime;
private OnShakeListener mShakeListener;
private Context mContext;
private int mShakeCount = 0;
private long mLastShake;
private long mLastForce;

public interface OnShakeListener {
public void onShake();
// public void onShakeHorizontal();
// public void onShakeVertical();
}

public ShakeListener(Context context) {
mContext = context;
resume();
}

public void setOnShakeListener(OnShakeListener listener) {
mShakeListener = listener;
}

public void resume() {
mSensorMgr = (SensorManager) mContext
.getSystemService(Context.SENSOR_SERVICE);
if (mSensorMgr == null) {
throw new UnsupportedOperationException("Sensors not supported");
}

boolean supported = mSensorMgr.registerListener(this, mSensorMgr
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
if (!supported) {
mSensorMgr.unregisterListener(this);
throw new UnsupportedOperationException(
"Accelerometer not supported");
}
}

public void pause() {
if (mSensorMgr != null) {
mSensorMgr.unregisterListener(this);
mSensorMgr = null;
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

@Override
public void onSensorChanged(SensorEvent event) {

if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
return;
}

long now = System.currentTimeMillis();

if ((now - mLastForce) > SHAKE_TIMEOUT) {
mShakeCount = 0;
}

if ((now - mLastTime) > TIME_THRESHOLD) {
long diff = now - mLastTime;
float speed = Math.abs(event.values[SensorManager.DATA_X]
+ event.values[SensorManager.DATA_Y]
+ event.values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ)
/ diff * 10000;
if (speed > FORCE_THRESHOLD) {
if ((++mShakeCount >= SHAKE_COUNT)
&& (now - mLastShake > SHAKE_DURATION)) {
mLastShake = now;
mShakeCount = 0;
if (mShakeListener != null) {
mShakeListener.onShake();
}
}
mLastForce = now;
}
mLastTime = now;
mLastX = event.values[SensorManager.DATA_X];
mLastY = event.values[SensorManager.DATA_Y];
mLastZ = event.values[SensorManager.DATA_Z];
}
}
}


使用的时候

ShakeListener mShaker = new ShakeListener(this);
mShaker.setOnShakeListener(new ShakeListener.OnShakeListener() {
public void onShake() {
// action while shaking
}
});


需要注意的是,在Activity的onPause和onResume函数里需要调用ShakeListener实例的pause和resume过程,注册和注销事件监听。
相关标签: Android UI