屏幕方向有问题。基本上我要做的是确定确切的屏幕方向(包括反向横向和反向纵向)。我在下面编写方法为我做这件事,它似乎在我的Transformer上工作得很好但是当我在模拟器上启动相同的代码时,有些东西搞砸了。
基本上发生的事情是,无论何时在模拟器中我根据文档切换到横向(ROTATION_90)它应该是反向横向(例如在Transformer with ROTATION_90设备上是反向纵向的,如Android文档所述)。不幸的是,在模拟器上由于某种原因它似乎是正常的横向模式。
所以我的问题是下面的代码正确,问题在于它。或者我可能没有考虑其他一些参数(比如仿真器几乎没有启用加速度计)。
public static int getDeviceCurrentOrientation() {
WindowManager lWindowManager = (WindowManager) getContext()
.getSystemService(Activity.WINDOW_SERVICE);
Configuration cfg = getContext().getResources().getConfiguration();
int lRotation = lWindowManager.getDefaultDisplay().getRotation();
int rotation1;
int orientation;
rotation1 = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
if (cfg.orientation == Configuration.ORIENTATION_LANDSCAPE)
rotation1 = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
else if (cfg.orientation == Configuration.ORIENTATION_PORTRAIT)
rotation1 = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
else
rotation1 = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
switch (lRotation) {
case Surface.ROTATION_0:
orientation = 0;
break;
case Surface.ROTATION_90:
orientation = 1;
break;
case Surface.ROTATION_180:
orientation = 2;
break;
case Surface.ROTATION_270:
orientation = 3;
break;
default:
orientation = -1;
}
if ((orientation == 0)
&& (rotation1 == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE))
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
if ((orientation == 0)
&& (rotation1 == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT))
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
if ((orientation == 1)
&& (rotation1 == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT))
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
if ((orientation == 1)
&& (rotation1 == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE))
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
if ((orientation == 2)
&& (rotation1 == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT))
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
if ((orientation == 2)
&& (rotation1 == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE))
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
if ((orientation == 3)
&& (rotation1 == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT))
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
if ((orientation == 3)
&& (rotation1 == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE))
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
}
答案 0 :(得分:2)
我有类似的问题,经过几个小时的嘎吱嘎吱...... 我找到了this nice solution >>
Display display = getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
Point size = new Point();
display.getSize(size);
int lock = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
if (rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) {
// if rotation is 0 or 180 and width is greater than height, we have
// a tablet
if (size.x > size.y) {
if (rotation == Surface.ROTATION_0) {
lock = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
lock = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
} else {
// we have a phone
if (rotation == Surface.ROTATION_0) {
lock = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} else {
lock = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
}
} else {
// if rotation is 90 or 270 and width is greater than height, we
// have a phone
if (size.x > size.y) {
if (rotation == Surface.ROTATION_90) {
lock = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
lock = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
} else {
// we have a tablet
if (rotation == Surface.ROTATION_90) {
lock = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
} else {
lock = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
}
}