我正在开发一款适用于平板电脑的应用,平板电脑将放置在一个可以围绕给定点旋转的支架上。这意味着首次打开应用时平板电脑的移动非常少。应用程序将根据平板电脑在支架中的旋转显示各种内容。我无法找到怎样做,无论我搜索多么难以获得与OrientationEventListener.onOrientationChanged(int orientation)
的值相当的度数或弧度的初始方向。我已经配置了OrientationEventListener,我的想法是在默认的活动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帖子,但没有到任何地方。
答案 0 :(得分:1)
查看来自getOrientationUsingGetRotationMatrix()
的{{1}}方法:Link:
DeviceOrientationService
检查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(...)
。