在android11 上实现平行视界效果

发布于:2024-07-13 ⋅ 阅读:(256) ⋅ 点赞:(0)

前言:

平行视界是谷歌为了解决大屏横屏设备 适配为手机等竖屏设备开发的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:


网站公告

今日签到

点亮在社区的每一天
去签到