gpt4 book ai didi

事件触发前的 Android SensorManager.getOrientation

转载 作者:行者123 更新时间:2023-11-30 03:16:00 26 4
gpt4 key购买 nike

我正在为平板电脑开发一个应用程序,平板电脑将放在一个围绕给定点旋转的支架上。这意味着当应用程序首次打开时,很少从平板电脑移动。该应用程序将根据平板电脑在支架上的旋转显示各种内容。无论我多么努力地搜索,我似乎都找不到如何做的是获得以度数或弧度为单位的初始方向,这与 OrientationEventListener.onOrientationChanged(int orientation) 的值相当。我配置了一个 OrientationEventListener,我的想法是在默认 Activities 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 帖子,却一无所获。

最佳答案

查看 DeviceOrientationService 中的 getOrientationUsingGetRotationMatrix() 方法: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 SensorManager.getOrientation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20131490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com