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

Pau*_*ell 6 android rotation sensor

我有一个非常简单的要求.假设一个设备站在它的末端,垂直于地面,并且它是倾斜的,我需要确定的是手机是向前还是向后倾斜(屏幕更朝向地面或更多朝向天花板).

我知道如何从各种传感器读取值,我认为使用传感器TYPE_ROTATION_VECTOR是前进的方向.我所缺少的是数学知识如何从它返回的三个值中确定前进或后退.

我在没有启发的情况下阅读了SO上的所有相关主题,非常感谢任何帮助.

Aji*_*jit 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.
Run Code Online (Sandbox Code Playgroud)

在你的情况下试试这个,

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


        }

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

        }
Run Code Online (Sandbox Code Playgroud)

加速度传感器