3

如何确定手机方向在一个平面上的角度?

现在我通过 SensorManager 实现了:

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

...

public void onSensorChanged(SensorEvent event) {
    xy_angle = event.values[0]; 
    xz_angle = event.values[1]; 
    zy_angle = event.values[2]; 

在这里我得到不同的角度,但我只需要一个角度,随着手机的旋转而变化,同时沿着蓝线行驶。

如何计算或如何获得这个角度?

http://i.stack.imgur.com/kCElj.jpg

如何将 SensorManager.getOrientation 用于“我的纸飞机”等倾斜控件?- 类似的问题在这里,但我不明白作者如何解决他的问题。

4

3 回答 3

2

你需要的是SensorManager.getOrientation().

于 2012-05-01T12:51:18.810 回答
2

I see that this post is a bit old, so maybe it's been answered by now, but Sensor.TYPE_ORIENTATION is now deprecated in favor of SensorManager.getOrientation().

I've done a full writeup of essentially what you're looking for here: http://blog.samsandberg.com/2015/05/10/tilt-for-android/

Hope it helps someone looking for the same thing!

于 2015-05-10T21:49:31.717 回答
0

该问题的作者通过切换参数顺序解决了他的问题:

 if (SensorManager.getRotationMatrix(m_rotationMatrix, null,
                                    m_lastMagFields, m_lastAccels)) {

至:

if (SensorManager.getRotationMatrix(m_rotationMatrix, null,
                                m_lastAccels, m_lastMagFields)) {
于 2012-05-01T13:15:10.340 回答