屏幕旋转代码

 private enum SensorStateChangeActions {
        WATCH_FOR_LANDSCAPE_CHANGES, SWITCH_FROM_LANDSCAPE_TO_STANDARD, WATCH_FOR_POTRAIT_CHANGES, SWITCH_FROM_POTRAIT_TO_STANDARD;
    }

    public void goFullScreen() {
        if(flimartAPP.isPortrait()) {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        }else {
            updateImmersive(getResources().getConfiguration(), true);
            updatePlayerLayout(getResources().getConfiguration());
        }


        mSensorStateChanges = SensorStateChangeActions.WATCH_FOR_LANDSCAPE_CHANGES;
        if (null == sensorEvent)
            initialiseSensor(true);
        else
            sensorEvent.enable();
    }

    public void shrinkToPotraitMode() {
        if(!flimartAPP.isPortrait()) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }else {
            updateImmersive(getResources().getConfiguration(), false);
            updatePlayerLayout(getResources().getConfiguration());
        }


        mSensorStateChanges = SensorStateChangeActions.WATCH_FOR_POTRAIT_CHANGES;
        if (null == sensorEvent)
            initialiseSensor(true);
        else
            sensorEvent.enable();
    }
    public void shrink() {

        updateImmersive(getResources().getConfiguration(), false);
        updatePlayerLayout(getResources().getConfiguration());

        mSensorStateChanges = SensorStateChangeActions.WATCH_FOR_POTRAIT_CHANGES;

        if (null == sensorEvent)
            initialiseSensor(true);
        else
            sensorEvent.enable();
    }

    private SensorStateChangeActions mSensorStateChanges;
    OrientationEventListener sensorEvent;

  public  void initialiseSensor(boolean enable) {
        sensorEvent = new OrientationEventListener(this,
                SensorManager.SENSOR_DELAY_NORMAL) {

            @Override
            public void onOrientationChanged(int orientation) {
            /*
             * This logic is useful when user explicitly changes orientation using player controls, in which case orientation changes gives no callbacks.
             * we use sensor angle to anticipate orientation and make changes accordingly.
             */

//                if (null != mSensorStateChanges
//                        && mSensorStateChanges == SensorStateChangeActions.WATCH_FOR_LANDSCAPE_CHANGES
//                        && ((orientation >= 60 && orientation <= 120) || (orientation >= 240 && orientation <= 300))) {
//
//                    mSensorStateChanges = SensorStateChangeActions.SWITCH_FROM_LANDSCAPE_TO_STANDARD;
//                } else if (null != mSensorStateChanges
//                        && mSensorStateChanges == SensorStateChangeActions.SWITCH_FROM_LANDSCAPE_TO_STANDARD
//                        && (orientation <= 40 || orientation >= 320)) {
//                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
//                    mSensorStateChanges = null;
//                    sensorEvent.disable();
//                } else if (null != mSensorStateChanges
//                        && mSensorStateChanges == SensorStateChangeActions.WATCH_FOR_POTRAIT_CHANGES
//                        && ((orientation >= 300 && orientation <= 359) || (orientation >= 0 && orientation <= 45))) {
//                    mSensorStateChanges = SensorStateChangeActions.SWITCH_FROM_POTRAIT_TO_STANDARD;
//                } else if (null != mSensorStateChanges
//                        && mSensorStateChanges == SensorStateChangeActions.SWITCH_FROM_POTRAIT_TO_STANDARD
//                        && ((orientation <= 300 && orientation >= 240) || (orientation <= 130 && orientation >= 60))) {
//                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
//                    mSensorStateChanges = null;
//                    sensorEvent.disable();
//                }

                if(orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
                    return;  //手机平放时,检测不到有效的角度
                }
                //只检测是否有四个角度的改变
                if( null != mSensorStateChanges &&
                        mSensorStateChanges == SensorStateChangeActions.WATCH_FOR_LANDSCAPE_CHANGES &&
                        orientation > 350 || orientation< 10 && (orientation > 170 && orientation < 190)) { //0度,竖屏

                    mSensorStateChanges = SensorStateChangeActions.SWITCH_FROM_LANDSCAPE_TO_STANDARD;
                }
                else if(null != mSensorStateChanges
                        && mSensorStateChanges == SensorStateChangeActions.WATCH_FOR_POTRAIT_CHANGES
                        && (orientation > 80 &&orientation < 100) && (orientation > 170 &&orientation < 190)) { //90度,横屏

                    mSensorStateChanges = SensorStateChangeActions.SWITCH_FROM_POTRAIT_TO_STANDARD;
                }
                else if(null != mSensorStateChanges &&
                        mSensorStateChanges == SensorStateChangeActions.SWITCH_FROM_POTRAIT_TO_STANDARD &&
                 (orientation > 80 &&orientation < 100) && (orientation > 170 &&orientation < 190) ) { //180度,竖屏
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
                    mSensorStateChanges = null;
                    sensorEvent.disable();
                }
                else if(null != mSensorStateChanges
                        && mSensorStateChanges == SensorStateChangeActions.SWITCH_FROM_LANDSCAPE_TO_STANDARD &&
                        orientation > 350 || orientation< 10 && orientation > 260 &&orientation < 280  ) { //270度,横屏
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
                    mSensorStateChanges = null;
                    sensorEvent.disable();
                }
                else {
                    return;
                }
            }
        };
        if (enable)
            sensorEvent.enable();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值