Android横屏180°旋转
Android landscape screen 180° rotation
我需要知道我的 Android 设备屏幕何时从一个横向旋转到另一个横向(rotation_90 到 rotation_270)。
在我的 Android 服务中,我重新实现了 onConfigurationChanged(Configuration newConfig)
以了解设备的旋转。但是这个方法只有在设备从ORIENTATION_PORTRAIT
旋转到ORIENTATION_LANDSCAPE
时才会被系统调用,如果从ORIENTATION_LANDSCAPE
(90°)旋转到另一个ORIENTATION_LANDSCAPE
则不会被系统调用] (270°) !!
在这种情况下,我如何被调用?
谢谢。
您可以使用以下代码将先前的方向保存为 int 成员变量:
int oldRotation = getWindowManager().getDefaultDisplay().getRotation();
然后检查设备是否从一种横向模式旋转到另一种横向模式。
if(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
int newRotation = getWindowManager().getDefaultDisplay().getRotation();
if(newRotation != oldRotation) {
// rotation from 90 to 270, or from 270 to 90
}
oldRotation = newRotation;
}
您可以为您的 activity 启用 OrientationEventListener。
OrientationEventListener mOrientationListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
Log.v(TAG, "Orientation changed to " + orientation);
if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
return;
}
int degrees = -1;
if (orientation < 45 || orientation > 315) {
Log.i(TAG, "Portrait");
} else if (orientation < 135) {
degrees = 90;
Log.i(TAG, "Landscape"); // This can be reverse landscape
} else if (orientation < 225) {
degrees = 180;
Log.i(TAG, "Reverse Portrait");
} else {
degrees = 270;
Log.i(TAG, "Reverse Landscape"); // This can be landscape
}
}
};
if (mOrientationListener.canDetectOrientation() == true) {
Log.v(TAG, "Can detect orientation");
mOrientationListener.enable();
} else {
Log.v(TAG, "Cannot detect orientation");
mOrientationListener.disable();
}
我需要知道我的 Android 设备屏幕何时从一个横向旋转到另一个横向(rotation_90 到 rotation_270)。
在我的 Android 服务中,我重新实现了 onConfigurationChanged(Configuration newConfig)
以了解设备的旋转。但是这个方法只有在设备从ORIENTATION_PORTRAIT
旋转到ORIENTATION_LANDSCAPE
时才会被系统调用,如果从ORIENTATION_LANDSCAPE
(90°)旋转到另一个ORIENTATION_LANDSCAPE
则不会被系统调用] (270°) !!
在这种情况下,我如何被调用? 谢谢。
您可以使用以下代码将先前的方向保存为 int 成员变量:
int oldRotation = getWindowManager().getDefaultDisplay().getRotation();
然后检查设备是否从一种横向模式旋转到另一种横向模式。
if(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
int newRotation = getWindowManager().getDefaultDisplay().getRotation();
if(newRotation != oldRotation) {
// rotation from 90 to 270, or from 270 to 90
}
oldRotation = newRotation;
}
您可以为您的 activity 启用 OrientationEventListener。
OrientationEventListener mOrientationListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
Log.v(TAG, "Orientation changed to " + orientation);
if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
return;
}
int degrees = -1;
if (orientation < 45 || orientation > 315) {
Log.i(TAG, "Portrait");
} else if (orientation < 135) {
degrees = 90;
Log.i(TAG, "Landscape"); // This can be reverse landscape
} else if (orientation < 225) {
degrees = 180;
Log.i(TAG, "Reverse Portrait");
} else {
degrees = 270;
Log.i(TAG, "Reverse Landscape"); // This can be landscape
}
}
};
if (mOrientationListener.canDetectOrientation() == true) {
Log.v(TAG, "Can detect orientation");
mOrientationListener.enable();
} else {
Log.v(TAG, "Cannot detect orientation");
mOrientationListener.disable();
}