LogicalDisplay::configureDisplayLocked

本文介绍了如何使用函数处理屏幕尺寸,根据DisplayDeviceInfo的参数调整逻辑主屏幕到目标设备的尺寸,并考虑了屏幕旋转、宽高比变化和居中显示的策略。重点在于屏幕适配和旋转计算的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个函数的主要作用是把当前的逻辑主屏幕尺寸,裁剪成入参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;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值