android14 橫屏模式下強(qiáng)制豎屏導(dǎo)致左右兩側(cè)有黑邊問(wèn)題簡(jiǎn)單記錄

系統(tǒng)自帶app和很多第三方應(yīng)用都有此現(xiàn)象。
實(shí)際上是因?yàn)閙anifest添加了 android:screenOrientation="portrait"
屬性,導(dǎo)致LetterBox顯示。顯示具體log如下

D/ActivityTaskManager: Show LetterBox: Window{820f20 u0 Splash Screen com.android.myapplication}, reason=FIXED_ORIENTATION

通過(guò)winscope也能看出這個(gè)是LetterBox。
簡(jiǎn)單說(shuō)下問(wèn)題的由來(lái)。

adb shell wm size
Physical size: 720x720

首先判斷當(dāng)前設(shè)備是橫屏還是豎屏。

        val configuration: Configuration = resources.configuration
        val orientation: Int = configuration.orientation
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            // 豎屏
            Log.d("MainActivity", "豎屏")
        } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 橫屏
            Log.d("MainActivity", "橫屏")
        }

log顯示是橫屏。

MainActivity            com.android.myapplication            D  橫屏

所以,由于設(shè)備是橫屏,但是manifest 添加了

android:screenOrientation="portrait",強(qiáng)制豎屏,

因此FIXED_ORIENTATION,觸發(fā)了Letterbox顯示流程。

// com.android.server.wm.LetterboxUiController#layoutLetterbox
                if (!Build.IS_USER) {
                    Slog.d(TAG,"Show LetterBox: " + w.toString()
                            + ", reason=" + getLetterboxReasonString(w));
                }

按這種方式去修改 int resizeMode = ActivityInfo.RESIZE_MODE_RESIZEABLE; //固定開(kāi)啟resizeable屬性 ,在當(dāng)前設(shè)備上無(wú)法解決黑邊問(wèn)題:https://blog.csdn.net/HuanWen_Cheng/article/details/141966932

這種情況下,隱藏LetterBox(這里我嘗試修改com.android.server.wm.LetterboxUiController#shouldShowLetterboxUi,直接返回false)也會(huì)顯示不全,所以我修改了resolveFixedOrientationConfiguration 里的數(shù)據(jù)。

參考這個(gè):https://blog.csdn.net/wq892373445/article/details/123863686

我直接修改為final float desiredAspectRatio = 0.0f;,注釋掉原本的邏輯。

// com.android.server.wm.ActivityRecord#resolveFixedOrientationConfiguration
        // Aspect ratio as suggested by the system. Apps requested mix/max aspect ratio will
        // be respected in #applyAspectRatio.
/*        final float desiredAspectRatio;
        if (isDefaultMultiWindowLetterboxAspectRatioDesired(newParentConfig)) {
            desiredAspectRatio = DEFAULT_LETTERBOX_ASPECT_RATIO_FOR_MULTI_WINDOW;
        } else if (letterboxAspectRatioOverride > MIN_FIXED_ORIENTATION_LETTERBOX_ASPECT_RATIO) {
            desiredAspectRatio = letterboxAspectRatioOverride;
        } else {
            desiredAspectRatio = computeAspectRatio(parentBounds);
        }*/
        // 強(qiáng)制 desiredAspectRatio 為 0,這樣 applyAspectRatio 幾乎什么都不會(huì)做。
        final float desiredAspectRatio = 0.0f;
        // Apply aspect ratio to resolved bounds
        mIsAspectRatioApplied = applyAspectRatio(resolvedBounds, containingBoundsWithInsets,
                containingBounds, desiredAspectRatio);

使用 adb shell dumpsys activity -a a com.android.myapplication
可以看到修改desiredAspectRatio值后activityAspectRatio值變化:

      areBoundsLetterboxed=true
        letterboxReason=FIXED_ORIENTATION
        activityAspectRatio=1.1483253
      areBoundsLetterboxed=true
        letterboxReason=FIXED_ORIENTATION
        activityAspectRatio=1.0

參考鏈接:
Android 12 - Letterbox 模式
強(qiáng)制app橫屏顯示或者豎屏顯示(動(dòng)態(tài))
Android之LetterBox介紹
Android11.0(R) MTK6771 平板橫屏方案修改(強(qiáng)制app橫屏 + 開(kāi)機(jī)logo/動(dòng)畫(huà)+關(guān)機(jī)充電橫屏 + RecoveryUI 橫屏)
Android 10 應(yīng)用顯示寬高比maxAspectRatio導(dǎo)致應(yīng)用區(qū)域顯示不全的問(wèn)題
Android全屏黑邊解決方案
手機(jī)app在平板上沒(méi)有全屏顯示
關(guān)于A(yíng)ndroid12第三方應(yīng)用左右兩側(cè)有黑邊問(wèn)題
Android 12.0 第三方應(yīng)用左右兩側(cè)未全屏有黑邊問(wèn)題解決
Android P應(yīng)用顯示寬高比maxAspectRatio使用及原理

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容