在v4.3之前遵守安卓屏幕方向锁定

时间:2013-09-23 11:40:55

标签: android

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

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

1 个答案:

答案 0 :(得分: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());
}

获取当前的屏幕方向更为复杂。我使用了这里提供的答案之一:

How do I get the CURRENT orientation (ActivityInfo.SCREEN_ORIENTATION_*) of an Android device?

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;
}