在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:

相关推荐
Senar4 分钟前
Web端选择本地文件的几种方式
前端·javascript·html
iuyou️5 分钟前
Spring Boot知识点详解
java·spring boot·后端
北辰浮光8 分钟前
[Mybatis-plus]
java·开发语言·mybatis
一弓虽18 分钟前
SpringBoot 学习
java·spring boot·后端·学习
南客先生21 分钟前
互联网大厂Java面试:RocketMQ、RabbitMQ与Kafka的深度解析
java·面试·kafka·rabbitmq·rocketmq·消息中间件
烛阴22 分钟前
UV Coordinates & Uniforms -- OpenGL UV坐标和Uniform变量
前端·webgl
ai大佬25 分钟前
Java 开发玩转 MCP:从 Claude 自动化到 Spring AI Alibaba 生态整合
java·spring·自动化·api中转·apikey
姑苏洛言26 分钟前
扫码小程序实现仓库进销存管理中遇到的问题 setStorageSync 存储大小限制错误解决方案
前端·后端
我想进大厂31 分钟前
图论---朴素Prim(稠密图)
数据结构·c++·算法·图论
我想进大厂35 分钟前
图论---Bellman-Ford算法
数据结构·c++·算法·图论