有没有稳定的方法来检测震动事件?
我尝试使用加速度计检测震动事件。 我发现当我连续摇晃手机时,价值差异 的加速度相当稳定。 但是当我旋转手机时,该值总是会有很大的变化(该值通常比“摇动而不旋转”大)。 我想关注摇动事件,而不是旋转事件。有办法解决吗 问题?
这是我的抖动检测代码
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 上?
I've tried to detect shaking event using accelerometer.
I've found that when I give a contiuous shake to the phone, the value difference
of the acceleration is quite stable.
But when I rotate the phone, there's always a big change on the value( the value is usually bigger than "shaking without rotation").
I want to focus on the shaking event, not the rotation event. Is there a way to solve
the problem?
here's my code for shaing detection
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();
is it possible to neglect the rotation event using accelerometer?
Or should I try to get the orientation change and do some computations
on the sumAcc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用过滤器来消除重力的影响,有某种 有关执行此操作的文档教程。我做了类似的事情,就我而言,我还试图检测其他动作,所以你的任务看起来更简单一些。我可以在下周一发布代码(如果我记得的话)。
祝你好运!
我正在编辑答案以添加您可以使用传感器TYPE_LINEAR_ACCELERATION 如果您使用的是 Android 2.3 或更高版本。这是一个普通的加速度传感器,它忽略了
重力,您绝对不需要检测震动事件。
问候
编辑 13/06 ->我答应发布我的代码来检测晃动,但相反,我认为发布 此链接,它提供了各种响应中的示例代码片段。我的代码与那里发布的代码非常相似。
祝你好运!
You should use a filter to eliminate the influence of the gravity, there is some sort of tutorial on the docs to do so. I did something similar, in my case I was also trying to detect other movements, so your task seems a bit simpler. I can post the code next monday (If I remember).
Good luck!
I'm editing the answer to add you can use a Sensor TYPE_LINEAR_ACCELERATION if you're on Android 2.3 or higher. That is a normal acceleration sensor which discards the effect of
gravity, which you definetely don't need to detect a shake event.
regards
Edit 13/06 -> I promised to post my code to detect shaking, but instead, I think it is a better idea to post this link, which gives sample pieces of code in a variety of responses. My code is very similar to one of those posted there.
Good luck!
从代码的角度来看,您需要实现 SensorListener:
您需要获取 SensorManager:
并使用所需的标志注册该传感器:
在 onSensorChange() 方法中,您确定是否是摇晃:
摇晃阈值是定义为:
还有一些其他方法来检测震动运动。看看这个 链接。(如果该链接不起作用或链接已失效,查看此网络存档。)。
谢谢。
From the code point of view, you need to implement the SensorListener:
You will need to acquire a SensorManager:
And register this sensor with desired flags:
In your onSensorChange() method, you determine whether it’s a shake or not:
The shake threshold is defined as:
There are some other methods too, to detect shake motion. look at this link.(If that link does not work or link is dead, look at this web archive.).
Thanks.