在android11 上实现平行视界效果

前言:

平行视界是谷歌为了解决大屏横屏设备 适配为手机等竖屏设备开发的APP , 在这类APP显示时 在横屏设备上不方便用户观看。

android 13 上平行视界的效果如下:

正文:

在android13前 ,各家有各自的解决方案,下面提供一种实现方案,如下:

frameworks/base/services/core/java/com/android/server/wm/DisplayContent.java

  • // ---------#--------------#-----------

  • // | # # |

  • // | # # |

  • // | # # |

  • // | # # | 如果 显示器是横屏, app 是竖屏 , 则将显示的宽高 等比例缩放

  • // | # # |

  • // | # # |

  • // ---------#--------------#-----------

private DisplayInfo updateDisplayAndOrientation(int uiMode, Configuration outConfig) {

// Use the effective "visual" dimensions based on current rotation

final int rotation = getRotation();

final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);

  • final int dw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;

  • final int dh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;

+// 如果 显示器是横屏, app 是竖屏 , 则将显示的宽高 等比例缩放

+// final int dw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;

+// final int dh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;

  • int dw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;

  • int dh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;

  • if( SystemProperties.getBoolean("persist.sys.landspace.display.portrait.app.enabled", false) ) {

  • int appOrientation = getOrientation();

  • if( rotated && appOrientation==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ){

  • dw = mBaseDisplayWidth*mBaseDisplayWidth/mBaseDisplayHeight;

  • dh = mBaseDisplayWidth;

  • Slog.d(TAG, "修改显示大小 dw "+dw +" dh "+dh );

  • }

  • }

// Update application display metrics.

final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(rotation);

final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();

@@ -4015,6 +4035,17 @@ class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowCo

int height = displayInfo.logicalHeight;

int top = (physHeight - height) / 2;

out.set(left, top, left + width, top + height);

+// 在display 是横屏, app 是竖屏时 计算 window 的边框

  • if( SystemProperties.getBoolean("persist.sys.landspace.display.portrait.app.enabled", false) ) {

  • int appOrientation = getOrientation();

  • if( rotated && appOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ){

  • int appWidth = mBaseDisplayWidth*mBaseDisplayWidth/mBaseDisplayHeight;

  • int appHeight = mBaseDisplayWidth;

  • out.set(0, 0, appWidth, appHeight);

  • Slog.d(TAG, "计算window Bounds "+out);

  • }

  • }

}

frameworks/base/services/core/java/com/android/server/wm/DisplayRotation.java

@@ -1191,6 +1213,14 @@ public class DisplayRotation {

if (isAnyPortrait(preferredRotation)) {

return preferredRotation;

}

+// app 请求竖屏 ,display 修改返回横屏

  • if( SystemProperties.getBoolean("persist.sys.landspace.display.portrait.app.enabled", false) ) {

  • if( sensorRotation == Surface.ROTATION_90 ){

  • return mLandscapeRotation;

  • }else if( sensorRotation == Surface.ROTATION_270){

  • return mSeascapeRotation;

  • }

  • }

return mPortraitRotation;

case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:

相关推荐
寻星探路9 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
崔庆才丨静觅10 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
你撅嘴真丑11 小时前
第九章-数字三角形
算法
曹牧11 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
passerby606111 小时前
完成前端时间处理的另一块版图
前端·github·web components
uesowys11 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
掘了11 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅11 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
ValhallaCoder11 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮11 小时前
AI 视觉连载1:像素
算法