确定设备的自然方向是在横向/纵向还是在横向/纵向渲染应用程序资源之间是有区别的。听起来您想要前者:
int getNaturalOrientation() {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display defaultDisplay = windowManager.getDefaultDisplay();
int rotation = defaultDisplay.getRotation();
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
if (display.getDisplayHeight() >= display.getDisplayWidth()) {
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
if (display.getDisplayHeight() < display.getDisplayWidth()) {
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
}
}
〜受Chromium src中的代码启发:https://chromium.googlesource.com/chromium/src/+/aca91b5d8a53d77e98021d560305a996864df5e8/content/public/android/javatests/src/org/chromium/content/browser/ScreenOrientationListenerTest.java
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句