屏幕方向锁定

是否有可靠的方法可以在所有Android设备上锁定屏幕方向?以下代码适用于我的Nexus S和其他手机,但由于某种原因ROTATION_90对应于Xoom上的SCREEN_ORIENTATION_REVERSE_PORTRAIT。


有什么方法可以可靠地将旋转映射到方向?


private void lockScreenOrientation() {

    if (!mScreenOrientationLocked) {

        final int orientation = getResources().getConfiguration().orientation;

        final int rotation = getWindowManager().getDefaultDisplay().getOrientation();


        if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {

            if (orientation == Configuration.ORIENTATION_PORTRAIT) {

                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            }

            else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {

                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

            }

        }

        else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {

            if (orientation == Configuration.ORIENTATION_PORTRAIT) {

                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

            }

            else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {

                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);

            }

        }


        mScreenOrientationLocked = true;

    }

}


private void unlockScreenOrientation() {

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

    mScreenOrientationLocked = false;

}

编辑:此代码旨在获取当前方向并将其锁定。方向被暂时锁定,然后释放给用户。


鸿蒙传说
浏览 610回答 3
3回答

陪伴而非守候

我略微修改了diyism的答案,以弥补在版本2.3之前不能使用reverse_landscape和reverse_portrait模式的事实private static void disableRotation(Activity activity){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; final int orientation = activity.getResources().getConfiguration().orientation;&nbsp; &nbsp; final int rotation = activity.getWindowManager().getDefaultDisplay().getOrientation();&nbsp; &nbsp; // Copied from Android docs, since we don't have these values in Froyo 2.2&nbsp; &nbsp; int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;&nbsp; &nbsp; int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;&nbsp; &nbsp; if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;&nbsp; &nbsp; &nbsp; &nbsp; SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;&nbsp; &nbsp; }&nbsp; &nbsp; if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (orientation == Configuration.ORIENTATION_PORTRAIT)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (orientation == Configuration.ORIENTATION_LANDSCAPE)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (orientation == Configuration.ORIENTATION_PORTRAIT)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (orientation == Configuration.ORIENTATION_LANDSCAPE)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}private static void enableRotation(Activity activity){&nbsp; &nbsp; activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);}

侃侃尔雅

要临时锁定屏幕,您可以轻松使用://developing for android tablets **<uses-sdk android:minSdkVersion="12" />**//works perfectly... **WATCH OUT**: look portrait to reverse-portrait on api level 13 :)currentActivity.setRequestedOrientation(currentActivity.getResources().getConfiguration().orientation);//to re-enable sensor, just do:currentActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);在显示对话框和进行重要的背景工作期间,将其用于临时屏幕锁定。确保currentActivity在您尝试访问它时是有效的,否则它将无法正常工作:)祝好运 :)
打开App,查看更多内容
随时随地看视频慕课网APP