Lua's Blog

Everythine will be fine.

0%

Android/AOSP强制应用横屏显示

原因/Why

  • 本文代码分析基于Android 9
  • Base on Android 9

近期开始一款机顶盒的开发,测试中发现安装一些应用(通常是只针对手机)的时候,由于应用设置仅支持竖屏,会导致在电视端显示异常。

Some Application which only support vertical works fine with Android phone,but Android TV

可通过本文方法使得仅支持竖屏的应用能够在电视端正常显示。

following the content to let these applications work well.

通过查看此代码得知,AOSP已经有相关内容支持

The source code shows how to let the application force orientation.

因此只需要进行如下修改即可
Just do this to support force orientcation.

修改代码的表达式值为true

Change the source code value to true

修改方法:calculateBounds

Modify the calculateBounds method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void calculateBounds(Rect out) {
// Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
final int orientation = mDisplayInfo.rotation;
boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
int width = mDisplayInfo.logicalWidth;
int left = (physWidth - width) / 2;
int height = mDisplayInfo.logicalHeight;
int top = (physHeight - height) / 2;
//To fix surface compute VisibleRegion err, when portrait app force displays
//on ROTATION_0.
if(mService.mPolicy.isDefaultOrientationForced()) {
left = ROTATION_0;
}
out.set(left, top, left + width, top + height);
}

再次编译framework代码即可

Build the AOSP again.