我尝试使用加速度计检测震动事件。我发现当我连续摇晃手机时,加速度的值差是相当稳定的。但是当我旋转手机时,值总是会有很大的变化(该值通常大于“摇动不旋转”)。我想专注于摇晃事件,而不是旋转事件。有没有办法解决这个问题?
这是我的阴影检测代码
public void onSensorChanged(SensorEvent event)
{
if (event.sensor.getType() == SensorManager.SENSOR_ACCELEROMETER)
{
nowTime = System.currentTimeMillis();
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
nowAcc = Math.sqrt(x*x+y*y+z*z);
accDiff = Math.abs(nowAcc - preAcc);
timeDiff = (nowTime - preTime);
// try to get the sum of 10 samplings of accDiff
tempAccDiff10 = tempAccDiff9;
tempAccDiff9 = tempAccDiff8;
tempAccDiff8 = tempAccDiff7;
tempAccDiff7 = tempAccDiff6;
tempAccDiff6 = tempAccDiff5;
tempAccDiff5 = tempAccDiff4;
tempAccDiff4 = tempAccDiff3;
tempAccDiff3 = tempAccDiff2;
tempAccDiff2 = tempAccDiff1;
tempAccDiff1 = accDiff;
sumAcc = tempAccDiff10+tempAccDiff9+tempAccDiff8+tempAccDiff7+tempAccDiff6+
tempAccDiff5+tempAccDiff4+tempAccDiff3+tempAccDiff2+tempAccDiff1;
Log.i("SSSS",String.valueOf(sumAcc));
//when I give the phone a big & continuous "shake", it returns
//a value about 30~40, but when I give the phone a small
//"rotation", it returns a value of 80~120
preAcc = nowAcc;
preTime = nowTime;
if (sumAcc>100)
{
SM.unregisterListener(sensorListener, sensor);
}
//123
}
}//end of onSensorChanged();
是否可以使用加速度计忽略旋转事件?或者我应该尝试改变方向并对 sumAcc 进行一些计算?