gpt4 book ai didi

android - 即使传感器读数没有变化,也会调用 OnSensorChanged

转载 作者:行者123 更新时间:2023-11-29 01:23:40 25 4
gpt4 key购买 nike

我将传感器用作 Sensor.TYPE_ACCELEROMETER。我正在按如下方式实现 OnSensorChanged() 方法:

    @Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
float[] vector;
vector = event.values.clone();
double normVector =
Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
vector[0] = vector[0] / (float) normVector;
vector[1] = vector[1] / (float) normVector;
vector[2] = vector[2] / (float) normVector;
angle = (int) Math.round(Math.toDegrees(Math.acos(vector[2])));
// do something with angle
}

但是,即使我的设备平放在 table 上并且我看到 angle 的稳定值 (7),也会调用此方法。为什么会这样?有什么方法可以确保仅当 angle 的读数发生变化时才调用此方法?

最佳答案

直接来自 documentation :

A sensor of this type measures the acceleration applied to the device (Ad). Conceptually, it does so by measuring forces applied to the sensor itself (Fs) using the relation: Ad = - ∑Fs / mass

In particular, the force of gravity is always influencing the measured acceleration: Ad = -g - ∑F / mass

For this reason, when the device is sitting on a table (and obviously not accelerating), the accelerometer reads a magnitude of g = 9.81 m/s^2

Similarly, when the device is in free-fall and therefore dangerously accelerating towards to ground at 9.81 m/s^2, its accelerometer reads a magnitude of 0 m/s^2.

关于android - 即使传感器读数没有变化,也会调用 OnSensorChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35294886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com