2

Android 4.3 引入了用于设置 screenOrientation 的 userLandscape、userPortrait 和 fullUser 值 - 以及相应的 SCREEN_ORIENTATION_XX 常量。这些基本上是说,如果屏幕方向被锁定,则关联的显示器不会发生自动屏幕旋转,但解锁后会起作用。

简单的问题:在 4.3 之前这是如何完成的?是否设置约束值并监视全局设置,以便在运行时更改屏幕方向以反映这一点。或者,还有更好的方法?

4

1 回答 1

2

我也很难找到答案。SENSOR_LANDSCAPE 似乎忽略了设备方向锁定,所以这就是我最终使用的。

// Is the device set to allow auto-rotation?
if (Settings.System.getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0) == 1)
{
    // Unlock rotation
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else
{
    // Lock rotation
    setRequestedOrientation(getScreenOrientation());
}

获取当前的屏幕方向有点复杂。我使用了此处提供的答案之一:

如何获取 Android 设备的当前方向 (ActivityInfo.SCREEN_ORIENTATION_*)?

private int getScreenOrientation()
{
    WindowManager winMan = (WindowManager)getSystemService(Activity.WINDOW_SERVICE);
    int rotation = winMan.getDefaultDisplay().getRotation();
    DisplayMetrics dm = new DisplayMetrics();
    winMan.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;
}
于 2013-10-30T21:44:21.627 回答