如何使用Android传感器事件确定设备是面朝上还是面朝下

6
我有一个非常简单的要求。假设设备垂直于地面,直立放置,并且倾斜了,我只需要确定手机是向前或向后倾斜(屏幕朝向地面或朝向天花板)。我知道如何从各种传感器读取值,我认为使用传感器TYPE_ROTATION_VECTOR是前进的方法。我所缺少的就是数学知识,以确定三个返回值中的前后倾斜情况。我已经阅读了所有相关的SO线程,但是没有得到启示,非常感谢任何帮助。
3个回答

3
float[] rotationMatrix = new float[9];
float[] inclinationMatrix = new float[9];
float[] accelerometer; // values from sensor
float[] magnetic; // values from sensor
SensorManager.getRotationMatrix(rotationMatrix, inclinationMatrix, accelerometer, magnetic) 
int inclination = (int) Math.round(Math.toDegrees(Math.acos(rotationMatrix[8])));

if (inclination < 90)
{
      // face up
}

if (inclination > 90)
{
       // face down
}

1
X轴是水平的,向右指;Y轴是垂直的,向上指;Z轴指向屏幕前面的外部。在这个系统中,屏幕后面的坐标具有负的Z值。
参考坐标系被定义为一个直接正交基,其中:
X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points East).
Y is tangential to the ground at the device's current location and points towards magnetic north.
Z points towards the sky and is perpendicular to the ground.

在你的情况下,请尝试这个:

if(Round(y,4) < 8.0){
           Log.d("sensor", "=====UP====");


        }

        else if(Round(y,4) < -8.0){
            Log.d("sensor", "=====DOWN====");

        }

accelerometer sensor


感谢您的快速回复。冒昧问一句,您的Round()函数是指什么?我可能比现在看起来还要蠢些。 - PaulJNewell

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接