我的应用程序使用相机。要以正确的方式显示相机的预览,我必须考虑相对于物理设备方向的 Activity 方向。 IE。如果 Activity 方向被锁定并且永远不会改变,我不需要采取任何进一步的步骤 - 随着设备旋转,预览图像将相应地旋转。但是,想象一下我的 Activity 可以改变方向。您旋转设备 - 并预览 - 直到达到纵向模式(假设它最初是横向),此时 Activity 旋转以适应新方向。但是预览图像随它旋转,现在它与相机和周围的现实不同步。我必须做的是确定 Activity 方向并相应地旋转图像。
Display.getRotation()
似乎可以用于此目的。但显然,它不能:https://groups.google.com/forum/#!topic/android-developers/ij_0QbApKKc
问题是原点不是由 Android API 固定的。一些平板电脑在正常方向(横向、音量按钮向上)返回旋转 0,而其他一些(比如我的 Nexus 7 2013)返回 1。
我该如何解决这个问题?
最佳答案
您可以使用 OrientationEventListener 确定方向就像这样
mOrientationEventListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
// determine our orientation based on sensor response
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if (display.getOrientation() == Surface.ROTATION_0) { // landscape oriented devices
isLandscapeOriented = true;
if (orientation >= 315 || orientation < 45) {
if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
}
} else if (orientation < 315 && orientation >= 225) {
if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
mOrientation = ORIENTATION_PORTRAIT_INVERTED;
}
} else if (orientation < 225 && orientation >= 135) {
if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
}
} else if (orientation <135 && orientation > 45) {
if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
mOrientation = ORIENTATION_PORTRAIT_NORMAL;
}
}
} else { // portrait oriented devices
if (orientation >= 315 || orientation < 45) {
if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
mOrientation = ORIENTATION_PORTRAIT_NORMAL;
}
} else if (orientation < 315 && orientation >= 225) {
if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
}
} else if (orientation < 225 && orientation >= 135) {
if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
mOrientation = ORIENTATION_PORTRAIT_INVERTED;
}
} else if (orientation <135 && orientation > 45) {
if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
}
}
}
}
};
}
if (mOrientationEventListener.canDetectOrientation()) {
mOrientationEventListener.enable();
}
不要忘记调用 mOrientationEventListener.disable();
编辑:
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
关于android - 如何获得相对于设备主体的 Activity 的真实旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26467370/