OrientationEventListener

OrientationEventListener

我正在为平板电脑开发一个应用程序,其中平板电脑将位于围绕给定点旋转的支架中。这意味着在首次打开应用程序时,平板电脑移动很小。该应用程序将根据支架中平板电脑的旋转情况显示各种内容。无论我多么努力地寻找,我似乎都找不到该怎么做的方法,就是以度或弧度为单位的初始方向与OrientationEventListener.onOrientationChanged(int orientation)的值相当。我配置了OrientationEventListener,我的想法是在默认的Activity onCreate方法期间,我将使用更新后的值手动调用OrientationEventListener。

演示代码不是自己运行的,而是说明了这一点:

//No problems at this point in the code, the OrientationManager class
//which extends OrientationEventListener properly receives onOrientationChanged
//notices
OrientationEventListener orientationEventListener;
orientationEventListener = new OrientationManager(
                               this,
                               SensorManager.SENSOR_DELAY_NORMAL
                           );
orientationEventListener.enable();

//Here's where the problem is... need to get the current orientation
//and "initialize" the OrientationEventListener
float[] coords = new float[3];
float[] rotation = new float[16];
SensorManager.getOrientation(rotation, coords);

//Test for correct coords values (logs "0.0, -0.0, -0.0")
Log.d("DEBUG", Float.toString(coords[0]) + ", "
               + Float.toString(coords[1]) + ", "
               + Float.toString(coords[2])
     );

//Goal is to be able to call orientationEventListener.onOrientationChanged(??)
//with the value that would be sent if the event was called naturally


现在,我是否提交了已经问过并回答过的问题?我看了这么多SO帖子,却一无所获。

最佳答案

看一下getOrientationUsingGetRotationMatrix()中的DeviceOrientationService方法:Link

private void getOrientationUsingGetRotationMatrix() {
    if (mGravityVector == null || mMagneticFieldVector == null) {
        return;
    }

    // Get the rotation matrix.
    // The rotation matrix that transforms from the body frame to the earth frame.
    float[] deviceRotationMatrix = new float[9];
    if (!SensorManager.getRotationMatrix(
            deviceRotationMatrix, null, mGravityVector, mMagneticFieldVector)) {
        return;
    }

    // Convert rotation matrix to rotation angles.
    // Assuming that the rotations are appied in the order listed at
    // http://developer.android.com/reference/android/hardware/SensorEvent.html#values
    // the rotations are applied about the same axes and in the same order as required by the
    // API. The only conversions are sign changes as follows.
    // The angles are in radians
    float[] rotationAngles = new float[3];
    SensorManager.getOrientation(deviceRotationMatrix, rotationAngles);
    double alpha = Math.toDegrees(-rotationAngles[0]);
    while (alpha < 0.0) { alpha += 360.0; } // [0, 360)
    double beta = Math.toDegrees(-rotationAngles[1]);
    while (beta < -180.0) { beta += 360.0; } // [-180, 180)
    double gamma = Math.toDegrees(rotationAngles[2]);
    while (gamma < -90.0) { gamma += 360.0; } // [-90, 90)

    maybeSendChange(alpha, beta, gamma);
}


检查rotation是否确实保存有效值,并且getRotationMatrix(...)返回true

关于android - 事件触发前的Android SensorManager.getOrientation,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20131490/

10-11 20:32