android – setRequestedOrientation,但仍然根据传感器改变

我想在处理屏幕方向方面模拟youtube视频查看行为.

用例:
P1.当用户按最大化时 – >活动总是进入景观
P2.当用户按最小化时 – >活动总是进入人像
P3.当用户旋转设备时 – >即使之前应用了p1或p2,屏幕方向也应相应改变.

目前我使用:

 @Override
 public void onClick(View view) {
     if (getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT) {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
     } else {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
     }

但是,这会永久锁定方向并失败p3.

解决方法:

玩了一会儿后,我找到了正确的解决方案.
首先 – 问题是android在setRequestedOrientation调用后锁定了屏幕,你不能混合使用两者,唯一要做的就是手动完成所有操作.

方法如下:

class PlayerOrientationListener extends OrientationEventListener {
    PlayerOrientationListener() {
        super(VideoPlayerActivity.this);
    }

    @Override
    public void onOrientationChanged(int orientation) {
        int threshold = 5;
        if (Math.abs(orientation - 0) < threshold) orientation = 0;
        else if (Math.abs(orientation - 90) < threshold) orientation = 90;
        else if (Math.abs(orientation - 180) < threshold) orientation = 180;
        else if (Math.abs(orientation - 270) < threshold) orientation = 270;

        switch (orientation) {
            case 0:
                if (!orientationLandscapeLocked) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    orientationPortraitLocked = false;
                }
                break;
            case 90:
                if (!orientationPortraitLocked) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                    orientationLandscapeLocked = false;
                }
                break;
            case 180:
                if (!orientationLandscapeLocked) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                    orientationPortraitLocked = false;
                }
                break;
            case 270:
                if (!orientationPortraitLocked) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    orientationLandscapeLocked = false;
                }
                break;
        }
    }
}

和活动代码:

View.OnClickListener onExpandClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
                orientationLandscapeLocked = true;
                orientationPortraitLocked = false;
            } else {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
                orientationPortraitLocked = true;
                orientationLandscapeLocked = false;
            }
        }
    };
btnExpandVideo.setOnClickListener(onExpandClick);

orientationListener = new PlayerOrientationListener();
orientationListener.enable();
上一篇:Android会检测来自网址的视频是否为纵向


下一篇:javascript – 为Phonegap / Cordova iOS应用程序强制执行正确的方向横向模式