android - 从应用程序平滑更改 SDK 的方向

标签 android orientation screen-orientation

我知道与方向相关的问题已被多次询问,但我面临一些奇怪的问题。 我正在开发一个 SDK,它具有一些可以从应用程序打开的 Activity ,该 SDK 要求应用程序提供方向值,并且我正在通过设置更新 Activity 的 onCreate() 中的方向:

requestedOrientation = orientation

当我将设备保持在与应用程序设置的方向相同的方向时,它可以完美地工作。但是,如果我以纵向模式握住设备并将 requestedOrientation 设置为 LANDSCAPE,那么我的屏幕会闪烁,首先显示纵向屏幕,然后转到风景。 在 list 中,我已将 configChanges 设置为 android:configChanges="keyboard|keyboardHidden|screenSize" 我对两个方向都有单独的布局。如果还需要其他任何信息,请告诉我。

最佳答案

But If I am holding the device in portrait mode and setting the requestedOrientation to LANDSCAPE then my screen flickers, 1st it shows the portrait screen and then goes to landscape

屏幕方向是一项 Activity 功能,不同的 Activity 可能会有所不同。因此,从一个 Activity 到另一个 Activity ,系统必须提前决定应该在哪个方向上显示该 Activity ;这是在 requestedOrientation 发生之前进行的;因为它显示了具有默认方向设置的 Activity ;然后当 requestedOrientation 被调用时,该 Activity 将按照所需的方向重新创建;所以 onCreate() 将会以不同的方向被调用两次;这就是为什么屏幕在显示横向(用户所需的方向)之前会以纵向(系统所需的方向)闪烁的原因。

In documentation ,默认的 android:screenOrientation 方向为“未指定”;即,由系统决定;

It works perfectly when I am holding the device in the same orientation as the orientation is set by App.

通常默认为设备传感器。

但是您可以指定为前面的第二个 Activity (在 list 的 Activity 部分中)“锁定”,这样系统就没有机会做出决定。

"locked": Locks the orientation to its current rotation, whatever that is. Added in API level 18.

<activity
    android:name="....."
    android:screenOrientation="locked"
    .... />

关于android - 从应用程序平滑更改 SDK 的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71420060/

相关文章:

java - 在重试谓词上执行并从 observable 返回值

ios - 为什么 shouldAutorotateToInterfaceOrientation 方法标记为不可用?

java.lang.RuntimeException : Unable to instantiate activity ComponentInfo java. lang.NullPointerException

带有自定义图像和进度指示器的 Android 加载进度条

java - Android 相机在纵向方向上拍摄横向照片

ios - 如何在 Swift 中以编程方式旋转方向?

android - DatePickerDialog 在屏幕旋转时保存布局

android - 如何检测设备何时从纵向模式切换到横向模式?

android - 如何在构建过程中从 .so 文件中删除符号

android - 如何更改软键盘的方向