0

我的屏幕方向有些问题。基本上我要做的是确定确切的屏幕方向(包括反向横向和反向纵向)。我在下面编写了方法来为我做这件事,它似乎在我的 Transformer 上完美地工作,但是当我在模拟器上启动相同的代码时,有些事情变得一团糟。

基本上发生的事情是,每当在模拟器中我根据文档切换到横向(ROTATION_90)时,它应该是反向横向(例如,在带有 ROTATION_90 设备的 Transformer 上,正如 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;
}
4

1 回答 1

2

我遇到了类似的问题,经过数小时的努力……
我找到了这个不错的解决方案>>

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;
        }
    }
}
于 2013-02-10T00:50:38.110 回答