Android:getAngleChange - 奇怪的结果
我目前正在试验 Android 手机的一些传感器。为了进行测试,我使用的是三星 Galaxy S。由于它没有陀螺仪,因此我使用加速度计和磁场传感器。
我基本上想做的是在移动设备时获得一定的角度。我尝试解释一下:假设您将手机以横向模式放在面前,然后将自己向右旋转 90 度。
我使用以下代码来获取当前的旋转矩阵:
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
mGravity = event.values.clone();
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mGeomagnetic = event.values.clone();
}
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity,
mGeomagnetic);
}
}
这效果很好,然后我使用 SensorManager.getAngleChange(angleChange, R, lastR); 来实现角度变化。 如果我按照上述方式转动自己并且不倾斜手机或更改任何其他内容,那么我会在 angleChange[1]
中获得(大约)65°...
但是如果我也将手机倾斜 90°当我转动自己时(以便显示器随后看向天花板),我在 angleChange[1]
中得到(大约)90°。
我现在很困惑,为什么这样的旋转会影响 angleChange[1]
中的值,另一方面为什么需要它才能获得预期的 90°。
我想要实现的是在如上所述移动手机时获得角度(不是以 90° 度的步骤,而是这种方向变化),无论进行哪些其他方向变化(沿着其他两个轴)。
有这种可能吗?
I'm currently experimenting with some sensors of Android phones. For testing, I'm using a Samsung Galaxy S. As it does not have a gyroscope, I'm using accelerometer and sensor for magnetic field.
What I basically want to do, is to get a certain angle, when moving the device. I try to explain: consider you are holding the phone in landscape mode in front of your face and then you turn yourself by 90 degrees to the right.
I use the following code to get the current rotation matrix:
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
mGravity = event.values.clone();
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mGeomagnetic = event.values.clone();
}
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity,
mGeomagnetic);
}
}
This works well and then I use SensorManager.getAngleChange(angleChange, R, lastR);
to achieve the angle change.
I then get (roughly) 65° in angleChange[1]
if I turn myself as described above and do not tilt the phone or change anything else...
But if I also tilt the phone by 90° when turning myself (so that display is looking to the ceiling afterwards) I get (roughly) 90° in angleChange[1]
.
I'm very confused now, why such a rotation affects the value in angleChange[1]
and on the other hand why it is needed to get the expected 90°.
What I want to achieve is to get the angle when moving the phone as described above (not in 90° degree steps but this sort of orientation change) no matter which other orientation changes (along the two other axes) are made.
Is there any possibility for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论