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

发布于 2025-01-12 21:53:33 字数 524 浏览 3 评论 0原文

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

requestedOrientation = orientation

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

I know the question related to orientation has been asked multiple times, but I am facing some weird issue.
I am working on one SDK that has some Activity which can be opened from the App, The SDK asks for the orientation values from the App and I am updating the orientation in onCreate() of the Activity by setting:

requestedOrientation = orientation

It works perfectly when I am holding the device in the same orientation as the orientation is set by App. 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.
In manifest I have set the configChanges to android:configChanges="keyboard|keyboardHidden|screenSize"
I have separate layouts for both the orientations. Let me know, if anything else is also required.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随波逐流 2025-01-19 21:53:33

但是,如果我将设备保持在纵向模式并将 requestOrientation 设置为横向,那么我的屏幕会闪烁,首先显示纵向屏幕,然后转到横向

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

在文档中,默认的 android: screenOrientation 方向为“未指定”;即,由系统决定;

当我将设备保持在与应用程序设置的方向相同的方向时,它可以完美地工作。

通常默认由设备传感器决定。

但是您可以指定为前面的第二个活动“锁定”(在清单的活动部分中),以便系统没有机会做出决定。

“锁定”:将方向锁定为其当前旋转,无论是什么。添加到 API 级别 18。

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

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

The screen orientation is an activity feature that can differ from activity to another. So, going from activity to another, the system has to decide ahead which orientation it should display the activity on; this is taken before requestedOrientation takes place; because it shows the activity with the default orientation setting; then when requestedOrientation get called, the activity is recreated with the desired orientation; so onCreate() will get called twice with different orientations; and that is why the screen flickers with the portrait orientation (the system desired one) before showing the landscape orientation (the user desired one).

In documentation, the default android:screenOrientation orientation is "unspecified"; i.e., it's left up to the system to decide;

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

Usually that defaults up to the device sensor.

But you can specify that to be "locked" for the second activity ahead (in the manifest's activity section) so that the system has no chance to decide that.

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

<activity
    android:name="....."
    android:screenOrientation="locked"
    .... />
浮生面具三千个 2025-01-19 21:53:33

您应该根据配置更改设置请求的方向

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else if...
}

如果您想同时支持纵向和反向纵向,请使用 SCREEN_ORIENTATION_SENSOR_PORTRAIT 来实现。

You should set requested orientation based on configuration changes

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else if...
}

If you want to support portrait and reverse portrait together, do it with SCREEN_ORIENTATION_SENSOR_PORTRAIT.

秋心╮凉 2025-01-19 21:53:33

我们最终实现了一个解决方案,如下所示,现在客户在他们的 manifest 文件中声明相同的 Activity,并根据他们的要求替换方向。

SDK Manifest:

<activity
        android:name=".ActivityName"
        android:configChanges="keyboard|keyboardHidden|screenSize"
        android:exported="false"
        android:label="@string/title_activity_login"
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden" />

Client Manifest:

<activity
        android:name="sdkPackage.ActivityName"
        tools:replace="android:screenOrientation"
        android:screenOrientation="sensorLandscape"/>

sdkPackage是SDK的完整包,基本上客户端现在覆盖android:screenOrientation 并将其设置为他们想要的任何方向。

We have implemented a solution at our end as below, Now the clients are declaring the same Activity in their manifest file and replacing the orientation as per their requirements.

SDK Manifest:

<activity
        android:name=".ActivityName"
        android:configChanges="keyboard|keyboardHidden|screenSize"
        android:exported="false"
        android:label="@string/title_activity_login"
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden" />

Client Manifest:

<activity
        android:name="sdkPackage.ActivityName"
        tools:replace="android:screenOrientation"
        android:screenOrientation="sensorLandscape"/>

sdkPackage is the complete package of SDK, basically the client is now overriding the android:screenOrientation and setting it to whatever orientation they want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文