6

私には非常に単純な要件があります。デバイスが地面に対して垂直に立てられており、傾いていると仮定すると、電話が前方または後方に傾いているかどうかを判断する必要があるだけです (画面が地面に向かっているのか、天井に向かっているのか)。

私はさまざまなセンサーから値を読み取る方法を知っており、センサー TYPE_ROTATION_VECTOR を使用することが今後の方法であると考えています。私が見逃しているのは、返される 3 つの値から前方または後方を判断するための数学のノウハウだけです。

SO に関するすべての関連スレッドを啓発なしで読みました。

4

3 に答える 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
}
于 2013-02-08T06:33:21.747 に答える
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====");

        }

加速度センサー

于 2013-01-23T10:36:03.500 に答える