Android旋转矢量传感器错误

时间:2014-01-25 22:30:36

标签: android android-sensors

我不是在问一个问题。我打算发布这个以防其他人遇到这个问题。如果您按照旋转传感器的Android API指南进行操作,则会遇到错误。特别是:java.lang.IllegalArgumentException,因为某些设备返回一个具有五个值的数组。你可以解决这个问题,因为你知道这一点,但无论如何这里是如何做到的:

    private int rotateVectLength;
    private float[] jRotateVectValues = null;
    public void onSensorChanged(SensorEvent event) {
        // we received a sensor event. it is a good practice to check
        // that we received the proper event
        if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
            // convert the rotation-vector to a 4x4 matrix. the matrix
            // is interpreted by Open GL as the inverse of the
            // rotation-vector, which is what we want.
            if(jRotateVectValues == null) {
                rotateVectLength = event.values.length;
                jRotateVectValues = new float[rotateVectLength];
            }
            for(int i = 0; i < rotateVectLength; i++) {
                jRotateVectValues[i] = event.values[i];
            }
            SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
        }
    }

希望这有助于某人。干杯!

文档确实指出了这一点:

values[0]: x*sin(θ/2) 
values[1]: y*sin(θ/2) 
values[2]: z*sin(θ/2) 
values[3]: cos(θ/2) 
values[4]: estimated heading Accuracy (in radians) (-1 if unavailable)

值[3],最初是可选的,将始终从SDK级别18开始提供。 values [4]是SDK级别18中添加的新值。

0 个答案:

没有答案