Qt for Android 刘海屏 获取安全区域(Safe area, Display Cutouts)

在.java文件重写onAttachedToWindow()函数
然后通过safeArea() 将上下左右的安全区域发出去

public native static void safeArea(int top, int left, int bottom, int right);

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    System.out.println("java -> on attached to window");
    if( android.os.Build.VERSION.SDK_INT >= 28 )
    {
//            // 获取屏幕安全边界
        WindowInsets windowInsets = getWindow().getDecorView().getRootWindowInsets();
        if( windowInsets != null )
        {
            DisplayCutout displayCutout = windowInsets.getDisplayCutout();
            if( displayCutout != null ) {
                int left = displayCutout.getSafeInsetLeft();
                int top = displayCutout.getSafeInsetTop();
                int right = displayCutout.getSafeInsetRight();
                int bottom = displayCutout.getSafeInsetBottom();
                safeArea(top, left, bottom, right);
            }
            else
            {
                System.out.println("java -> displayCutout is null");
            }
        }
        else
        {
            System.out.println("java -> windowInsets is null");
        }
    }
}

Qt .cpp里面接收safeArea()

static int cutOutTop = 0;
static int cutOutLeft = 0;
static int cutOutBottom = 0;
static int cutOutRight = 0;

#ifdef Q_OS_ANDROID
#ifdef __cplusplus
extern "C" {
#endif

// Java_org_qtproject_example_function_ 为.java文件package字段
// 后面safeArea 为.java文件的方法名
JNIEXPORT void JNICALL Java_org_qtproject_example_function_safeArea(JNIEnv */*env*/,
                                                                    jobject /*obj*/,
                                                                    int top, int left, int bottom, int right)
{
    cutOutTop = top;
    cutOutLeft = left;
    cutOutBottom = bottom;
    cutOutRight = right;
}

#ifdef __cplusplus
}
#endif
#endif

获取到的安全区域值为分辨率的大小
非物理屏幕大小
转换为物理屏幕大小

QScreen *screen = QGuiApplication::primaryScreen();
int top = cutOutTop / screen->devicePixelRatio();
int left = cutOutLeft / screen->devicePixelRatio();
int bottom = cutOutBottom / screen->devicePixelRatio();
int right = cutOutRight / screen->devicePixelRatio();
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值