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

相关推荐
砍材农夫1 天前
spring-ai 第四多模态API
java·人工智能·spring
她说..1 天前
Java 对象相关高频面试题
java·开发语言·spring·java-ee
汀、人工智能1 天前
[特殊字符] 第21课:最长有效括号
数据结构·算法·数据库架构·图论·bfs·最长有效括号
Boop_wu1 天前
[Java 算法] 字符串
linux·运维·服务器·数据结构·算法·leetcode
庞轩px1 天前
深入理解 sleep() 与 wait():从基础到监视器队列
java·开发语言·线程··wait·sleep·监视器
徐小夕1 天前
我用 AI 撸了个开源"万能预览器":浏览器直接打开 Office、CAD 和 3D 模型
前端·vue.js·github
故事和你911 天前
洛谷-算法1-2-排序2
开发语言·数据结构·c++·算法·动态规划·图论
小码哥_常1 天前
Flutter Android 延迟加载代码指南:提升应用性能的关键
前端
Fcy6481 天前
算法基础详解(三)前缀和与差分算法
算法·前缀和·差分
皮皮林5511 天前
面试官:ZSet 的底层实现是什么?
java