为了账号安全,请及时绑定邮箱和手机立即绑定

屏幕方向锁定

/ 猿问

屏幕方向锁定

慕用2447696 2019-10-18 14:40:30

是否有可靠的方法可以在所有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;

}

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


查看完整描述

3 回答

?
aluckdog

这是我的解决方案,它适用于任何Android SDK中的手机和平板电脑。


switch (getResources().getConfiguration().orientation){

        case Configuration.ORIENTATION_PORTRAIT:

            if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO){

                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            } else {

                int rotation = getWindowManager().getDefaultDisplay().getRotation();

                if(rotation == android.view.Surface.ROTATION_90|| rotation == android.view.Surface.ROTATION_180){

                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

                } else {

                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

                }

            }   

        break;


        case Configuration.ORIENTATION_LANDSCAPE:

            if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO){

                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

            } else {

                int rotation = getWindowManager().getDefaultDisplay().getRotation();

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

                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

                } else {

                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);

                }

            }

        break;

    }


查看完整回答
反对 回复 2019-10-18
?
陪伴而非守候

我略微修改了diyism的答案,以弥补在版本2.3之前不能使用reverse_landscape和reverse_portrait模式的事实


private static void disableRotation(Activity activity)

{       

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

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


    // Copied from Android docs, since we don't have these values in Froyo 2.2

    int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;

    int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;


    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)

    {

        SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

        SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

    }


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

    {

        if (orientation == Configuration.ORIENTATION_PORTRAIT)

        {

            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        }

        else if (orientation == Configuration.ORIENTATION_LANDSCAPE)

        {

            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        }

    }

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

    {

        if (orientation == Configuration.ORIENTATION_PORTRAIT) 

        {

            activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);

        }

        else if (orientation == Configuration.ORIENTATION_LANDSCAPE) 

        {

            activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);

        }

    }

}


private static void enableRotation(Activity activity)

{

    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

}


查看完整回答
反对 回复 2019-10-18
?
侃侃尔雅

要临时锁定屏幕,您可以轻松使用:


//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在您尝试访问它时是有效的,否则它将无法正常工作:)


祝好运 :)


查看完整回答
反对 回复 2019-10-18

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信