Android-如何在不锁定屏幕的情况下通过编程实现屏幕旋转

7
我想要实现以下行为:
  • 当我将设备旋转到横屏时,屏幕也会跟着旋转。
  • 要将其恢复为纵向,请使用以下两种方法之一:
    1. 旋转设备
    2. 在仅在横屏模式下出现的按钮上单击,它将把屏幕旋转回纵向。
问题是: 将屏幕恢复为纵向的按钮功能良好,但我希望能够将设备旋转到横屏并旋转屏幕,但它仍然被锁定在纵向。
这个行为类似于YouTube播放器,您可以旋转或点击退出全屏按钮。
我的按钮代码:
findViewById(R.id.exit_fs).setOnClickListener(new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
        setRequestedOrientation(Build.VERSION.SDK_INT < 9 ?
        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :
        ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }
});

有什么想法吗?
6个回答

5
我发现这种方法非常好:
// E.g. fullscreen button pressed - set landscape orientation;
// it will disable orientation by the sensor.
setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE)

// Assume now user rotated the device... or not.

// Anyway enable orientation by the sensor after few seconds, so user may use app normal way.
someView.postDelayed(
    { setRequestedOrientation(SCREEN_ORIENTATION_SENSOR) }, delay = 5000L)

2
我曾经遇到过完全相同的问题。最终,我使用了一个OrientationListener来检测用户何时将手机实际倾斜到横向,并将方向设置为SCREEN_ORIENTATION_SENSOR。"最初的回答"。
OrientationEventListener orientationEventListener = new OrientationEventListener(getActivity()) {
@Override
public void onOrientationChanged(int orientation) {
    int epsilon = 10;
    int leftLandscape = 90;
    int rightLandscape = 270;
    if(epsilonCheck(orientation, leftLandscape, epsilon) ||
       epsilonCheck(orientation, rightLandscape, epsilon)){
            getMainActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        }
    }

    private boolean epsilonCheck(int a, int b, int epsilon) {
        return a > b - epsilon && a < b + epsilon;
    }
};
orientationEventListener.enable();

1

首先在Android设置中打开自动旋转。

然后在配置视频为横向模式的Activity中使用以下代码。

override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig)

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        // if you tilt your screen to Portrait mode
         // send your seek position with intent to continue watching video to your previous screen
           finish()
    }else{
       // continue to playing video
    }
}

在您的清单文件中,将screenOrientation设置为"fullSensor",如下所示:
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
        android:screenOrientation="fullSensor"

只需添加android:screenOrientation="fullSensor"以及打开自动旋转功能,问题就得到了解决。 - vianna77

0
在您的清单文件中,将screenOrientation设置为"fullSensor",如下所示:
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode" android:screenOrientation="fullSensor"

它对我有效。请尝试并评论... - Rohan Goyal

-1

阅读这个用户轮换

使用这个:

android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.USER ROTATION,user_rotation);

user_rotation 可以为 0、1、2 或 3;

  • 0 == 0º
  • 1 == 90º
  • 2 == 180º
  • 3 == 270º

您需要在清单文件中添加下一行。

<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>

提示:如果你尝试过搜索,你本可以找到一些相关的内容。比如这篇Riddhish.Chaudhari的帖子


谢谢你的回复,维克多。但是这对我根本没用。也许我使用方法有误,我把那段代码放在了OnClickListener中。你能否给我更多的细节?谢谢。(我已经搜索过了,但是找到的都一样) - facumedica
你正在将屏幕锁定在竖屏模式下,使用的代码为setRequestedOrientation(Build.VERSION.SDK_INT < 9 ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT)。 - Víctor Martín
我不是很清楚,当你处于横屏模式时会发生什么。 - Víctor Martín
我没有使用我的旧代码,只是你发布的代码,它做了同样的事情。 好的,我会尽力更好地解释一下: 屏幕的自动旋转必须始终正常工作。此外,在横向模式下,我有一个按钮可以将屏幕旋转回纵向。 希望我解释得更清楚了。感谢您的关注! - facumedica
我刚刚给你发布了另一个答案。 - Víctor Martín
嗨 @facumedica,你找到解决方案了吗?实际上,我的需求和你的完全一样,我还是找不到一个合适的解决办法。 - Arunabha Dutta Choudhury

-1

尝试使用一种方法来启用/禁用自动旋转,在更改屏幕旋转之前/之后。 代码:

import android.provider.Settings;

public static void setAutoOrientationEnabled(Context context, boolean enabled)
        {
              Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
        }

在旋转按钮之前尝试启用自动旋转


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接