Zol*_*tán 56 android screen-orientation
我想找出一个装置的详细取向,优选的一个SCREEN_ORIENTATION_LANDSCAPE,SCREEN_ORIENTATION_PORTRAIT,SCREEN_ORIENTATION_REVERSE_LANDSCAPE,SCREEN_ORIENTATION_REVERSE_PORTRAIT从ActivityInfo或等同物.
StackOverflow中的一些答案包括在内
getWindowManager().getDefaultDisplay().getRotation()
Run Code Online (Sandbox Code Playgroud)
但这并没有真正告诉我该设备是处于纵向还是横向模式,只是它是如何根据其自然位置转动的 - 而这反过来可以是风景或肖像.
getResources().getConfiguration().orientation
Run Code Online (Sandbox Code Playgroud)
返回以下三种:ORIENTATION_LANDSCAPE,ORIENTATION_PORTRAIT,ORIENTATION_SQUARE,然后并没有真正告诉我哪种方式手机处于开机状态(无论是上下颠倒或两侧的它的转向).
我知道我可以结合使用后者DisplayMetrics来找出设备的自然方向,但是真的没有更好的方法吗?
Zol*_*tán 89
我最终使用以下解决方案:
private int getScreenOrientation() {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
int orientation;
// if the device's natural orientation is portrait:
if ((rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) && height > width ||
(rotation == Surface.ROTATION_90
|| rotation == Surface.ROTATION_270) && width > height) {
switch(rotation) {
case Surface.ROTATION_0:
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
case Surface.ROTATION_90:
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
case Surface.ROTATION_180:
orientation =
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
break;
case Surface.ROTATION_270:
orientation =
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
default:
Log.e(TAG, "Unknown screen orientation. Defaulting to " +
"portrait.");
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
}
}
// if the device's natural orientation is landscape or if the device
// is square:
else {
switch(rotation) {
case Surface.ROTATION_0:
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
case Surface.ROTATION_90:
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
case Surface.ROTATION_180:
orientation =
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case Surface.ROTATION_270:
orientation =
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
break;
default:
Log.e(TAG, "Unknown screen orientation. Defaulting to " +
"landscape.");
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
}
}
return orientation;
}
Run Code Online (Sandbox Code Playgroud)
注意:一些用户(Geltrude和holtaf在下面的评论中)指出,这种解决方案不适用于所有设备,因为自然方向的旋转方向未标准化.
Nil*_*esh 21
简单的方法就是使用
getResources().getConfiguration().orientation
Run Code Online (Sandbox Code Playgroud)
1表示Potrait,2表示Landscape.
Uma*_*han 15
public static int getScreenOrientation(Activity activity) {
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int orientation = activity.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) {
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} else {
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
}
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
}
return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
}
Run Code Online (Sandbox Code Playgroud)
我认为您的问题是您可以检测横向和纵向,但不能反转横向和反转版本,因为旧版本不支持它们.要检测你可以做的是你可以使用oreintation和rotation.我告诉你一个可能对你有用的想法.
试试这个我认为它可以解决你的问题.
int orientation = getResources().getConfiguration().orientation;
int rotation = getWindowManager().getDefaultDisplay().getRotation();
int actual_orientation = -1;
if (orientation == Configuration.ORIENTATION_LANDSCAPE
&& (rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_90)){
orientation = Configuration.ORIENTATION_LANDSCAPE;
} else if (orientation == Configuration.ORIENTATION_PORTRAIT
&& (rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_90)) {
orientation = Configuration.ORIENTATION_PORTRAIT;
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE
&& (rotation == Surface.ROTATION_180
|| rotation == Surface.ROTATION_270)){
orientation = //any constant for reverse landscape orientation;
} else {
if (orientation == Configuration.ORIENTATION_PORTRAIT
&& (rotation == Surface.ROTATION_180
|| rotation == Surface.ROTATION_270)){
orientation = //any constant for reverse portrait orientation;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
56187 次 |
| 最近记录: |