setRequestedOrientation

setRequestedOrientation

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

用例:
p1 。当用户按下最大化-> Activity 总是进入横向
p2 。当用户按下最小化-> Activity 总是进入纵向
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;
        }
    }
}

和 Activity 代码:
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 - setRequestedOrientation,但仍根据传感器而变化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46953691/

10-12 06:27