HarmonyOS NEXT应用开发之适配挖孔屏案例

介绍

本示例介绍使用屏幕属性getDefaultDisplaySync、getCutoutInfo接口实现适配挖孔屏。该场景多用于沉浸式场景下。

效果图预览

使用说明

  1. 加载完成后顶部状态栏时间和电量显示位置规避了不可用区域。

实现思路

  1. 通过setWindowLayoutFullScreen、setWindowSystemBarEnable将窗口设置为全屏,并且隐藏顶部状态栏。源码参考DiggingHoleScreen.ets

    // 获取窗口实例
    window.getLastWindow(this.context, (err, data) => {
    if (err) {
    logger.error('DiggingHoleScreen', 'getLastWindow failed. error is:', JSON.stringify(err));
    return;
    }
    // 设置窗口为全屏显示状态
    data.setWindowLayoutFullScreen(true);
    // 设置顶部状态栏为隐藏状态
    data.setWindowSystemBarEnable(['navigation']);
    });

  2. 通过getDefaultDisplaySync、getCutoutInfo获取窗口display对象和不可用区域的边界、宽高。源码参考DiggingHoleScreen.ets

    this.displayClass = display.getDefaultDisplaySync();
    this.displayClass.getCutoutInfo((err, data) => {
    if (err) {
    logger.error('DiggingHoleScreen', 'getCutoutInfo failed. error is:', JSON.stringify(err));
    return;
    }
    this.boundingRect = data.boundingRects;
    this.topTextMargin = this.getBoundingRectPosition();
    });

  3. 使用获取到的信息进行计算偏移量实现对不可用区域的适配。源码参考DiggingHoleScreen.ets

    getBoundingRectPosition(): TextMargin {
    if (this.boundingRect !== null && this.displayClass !== null && this.boundingRect[0] !== undefined) {
    // 不可用区域右侧到屏幕右边界的距离:屏幕宽度减去左侧宽度和不可用区域宽度
    let boundingRectRight: number = this.displayClass.width - (this.boundingRect[0].left + this.boundingRect[0].width);
    // 不可用区域左侧到屏幕左边界的距离:getCutoutInfo接口可以直接获取
    let boundingRectLeft: number = this.boundingRect[0].left;
    // 部分设备不可用区域在中间时存在左右距离会有10像素以内的差距,获取到的左右距离差值绝对值小于10都按照不可用区域位于中间处理
    if (Math.abs(boundingRectLeft - boundingRectRight) <= 10) {
    return { left: 0, right: 0 };
    }
    if (boundingRectLeft > boundingRectRight) {
    // 不可用区域在右边
    return { left: 0, right: this.displayClass.width - boundingRectLeft };
    } else if (boundingRectLeft < boundingRectRight) {
    // 不可用区域在左边
    return { left: this.boundingRect[0].left + this.boundingRect[0].width, right: 0 };
    }
    }
    return { left: 0, right: 0 };
    }

高性能知识点

不涉及

工程结构&模块类型

复制代码
functionalscenes                                // har类型(默认使用har类型,如果使用hsp类型请说明原因)
|---mainpage
|   |---DigginHoleScreen.ets                    // 挖孔屏适配页面

模块依赖

  1. 路由模块:供entry模块实现路由导航
  2. utils模块:功能介绍

参考资料

@ohos.display (屏幕属性)

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ......

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ......

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ......

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题

2.性能优化方向

3.架构方向

4.鸿蒙开发系统底层方向

5.鸿蒙音视频开发方向

6.鸿蒙车载开发方向

7.鸿蒙南向开发方向

腾讯T10级高工技术,安卓全套VIP课程全网免费送:https://qr21.cn/D2k9D5

相关推荐
憨波个15 小时前
【语音识别】Conformer: Convolution-augmented Transformer for Speech Recognition
人工智能·深度学习·transformer·语音识别
动物园猫16 小时前
睡岗检测数据集分享(适用于YOLO系列深度学习分类检测任务)
深度学习·yolo·分类
想你依然心痛16 小时前
HarmonyOS 6(API 23)智能体驱动的沉浸式AR深海科考探索舱
华为·ar·harmonyos·智能体
Goway_Hui16 小时前
【鸿蒙原生应用开发--ArkUI--002】CalculatorApp - 计算器应用教程
华为·harmonyos
大模型最新论文速读16 小时前
05-29 · LLM 最新论文速览
论文阅读·人工智能·深度学习·机器学习·自然语言处理
杀生丸学AI16 小时前
【三维重建】RT-Splatting:基于3DGS的联合反射-透射建模(CVPR 2026)
人工智能·深度学习·3d·三维重建·高斯泼溅·动态重建·镜面反射
weixin_4684668516 小时前
图像分类技术落地应用与实战指南
人工智能·深度学习·ai·分类·数据挖掘·图像分类·模型部署
MediaTea16 小时前
人工智能通识课:深度学习
人工智能·深度学习
Goway_Hui17 小时前
【鸿蒙原生应用开发--ArkUI--006】WeatherApp - 天气应用教程
华为·harmonyos
bylander17 小时前
【技术调研】华为《智能世界2035》白皮书调研报告
人工智能·华为