这个函数的主要作用是把当前的逻辑主屏幕尺寸,裁剪成入参DisplayDevice device的屏幕大小。
int orientation = Surface.ROTATION_0;
if ((displayDeviceInfo.flags & DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT) != 0) {
orientation = displayInfo.rotation;
}
// Apply the physical rotation of the display device itself.
orientation = (orientation + displayDeviceInfo.rotation) % 4;
displayInfo.rotation是mPrimaryDisplayDevice的旋转值,displayDeviceInfo.rotation是入参即目的屏幕的旋转值;360度/90度=4,所以两次旋转叠加后余除4。
接着看,
int displayRectWidth, displayRectHeight;
if ((displayInfo.flags & Display.FLAG_SCALING_DISABLED) != 0) {
displayRectWidth = displayInfo.logicalWidth;
displayRectHeight = displayInfo.logicalHeight;
} else if (physWidth * displayInfo.logicalHeight
< physHeight * displayInfo.logicalWidth) {
// Letter box.
displayRectWidth = physWidth;
displayRectHeight = displayInfo.logicalHeight * physWidth / displayInfo.logicalWidth;
} else {
// Pillar box.
displayRectWidth = displayInfo.logicalWidth * physHeight / displayInfo.logicalHeight;
displayRectHeight = physHeight;
}
如果目标宽高比<源宽高比,目标宽不变,高=目标宽/源宽高比;
反之,目标高不变,宽=目标高*源宽高比。
继续看这里,除2是为了居中显示:
int displayRectTop = (physHeight - displayRectHeight) / 2;
int displayRectLeft = (physWidth - displayRectWidth) / 2;