HarmonyOS NEXT实战:沉浸式效果工具

##HarmonyOS Next实战##HarmonyOS SDK应用服务##教育##

目标:封装工具类,实现沉浸式效果。

典型应用全屏窗口UI元素包括状态栏、应用界面和底部导航条,其中状态栏和导航条,通常在沉浸式布局下称为避让区;避让区之外的区域称为安全区。开发应用沉浸式效果主要指通过调整状态栏、应用界面和导航条的显示效果来减少状态栏导航条等系统界面的突兀感,从而使用户获得最佳的UI体验。

开发应用沉浸式效果主要要考虑如下几个设计要素:

  • UI元素避让处理:导航条底部区域可以响应点击事件,除此之外的可交互UI元素和应用关键信息不建议放到导航条区域。状态栏显示系统信息,如果与界面元素有冲突,需要考虑避让状态栏。
  • 沉浸式效果处理:将状态栏和导航条颜色与界面元素颜色相匹配,不出现明显的突兀感。

实战:

import { Rect } from **********';
import { window } from **********';
import { BusinessError } from **********';
import { UIAbility } from **********';
import { KeyboardAvoidMode } from **********';

export namespace StageModelKit {
  export class StageModel {
    static UIAbility: Map<string, UIAbility> = new Map<string, UIAbility>();
    static UIAbilityContext: Map<string, Context> = new Map<string, Context>();
    static WindowStage: Map<string, window.WindowStage> = new Map<string, window.WindowStage>();

    /**
     * 登记
     * @param UIAbilityContext
     * @param WindowStage
     */
    static register(UIAbilityContext: Map<string, Context>, WindowStage: Map<string, window.WindowStage>) {
      UIAbilityContext.forEach((value: Context, key: string, map: Map<string, Context>) => {
        StageModel.UIAbilityContext.set(key, value)
      })

      WindowStage.forEach((value: window.WindowStage, key: string, map: Map<string, window.WindowStage>) => {
        StageModel.WindowStage.set(key, value)
      })
    }
  }

  export class Window {
    private windowStageName: string;
    windowStage: window.WindowStage;
    avoidArea: AvoidArea;
    keyboardHeight: number;

    constructor(windowStageName: string) {
      this.windowStageName = windowStageName
      this.windowStage = new Object() as window.WindowStage
      const zeroRect: Rect = {
        left: 0,
        top: 0,
        width: 0,
        height: 0
      }
      this.avoidArea = new AvoidArea(zeroRect, zeroRect)
      this.keyboardHeight = 0
    }

    init() {
      //初始化 windowStage
      const windowStage = StageModel.WindowStage.get(this.windowStageName)
      if (windowStage) {
        this.windowStage = windowStage
      } else {
        throw new Error(`[异常][未获取到windowStage,请检查StageModel和windowStageName是否正确引用] windowStage is ${JSON.stringify(windowStage)}`)
      }
      //初始化 avoidArea
      const getWindow = this.windowStage.getMainWindowSync(); // 获取应用主窗口
      const topRect = getWindow.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect // 系统状态栏顶部区域
      const bottomRect =
        getWindow.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR).bottomRect // 导航条底部区域
      this.avoidArea = new AvoidArea(rect_px2vp(topRect), rect_px2vp(bottomRect))
    }

    /**
     * 沉浸式效果
     */
    setImmersiveEffect() {
      this.watchAvoidArea()
      this.watchKeyboardHeight()
      this.setFullScreen()
      // 设置虚拟键盘抬起时压缩页面大小为减去键盘的高度
      this.windowStage.getMainWindowSync().getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.RESIZE);
    }

    /**
     * 监控避让区
     */
    watchAvoidArea() {
      this.windowStage.getMainWindowSync().on('avoidAreaChange', (data) => {
        if (data.type === window.AvoidAreaType.TYPE_SYSTEM) {
          let avoidArea = this.avoidArea as AvoidArea
          avoidArea.topRect = rect_px2vp(data.area.topRect)
          this.avoidArea = avoidArea
        } else if (data.type == window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) {
          let avoidArea = this.avoidArea as AvoidArea
          avoidArea.bottomRect = rect_px2vp(data.area.bottomRect)
          this.avoidArea = avoidArea
        }else if (data.type == window.AvoidAreaType.TYPE_KEYBOARD) {
          // this.keyboardHeight = px2vp(data.area.bottomRect.height) //键盘高度
          // DeepalLogUtils.debug(`[日志]watchAvoidArea, keyboardHeight=${JSON.stringify(this.keyboardHeight)}`);
        }
      });
    }

    /**
     * 监控软键盘高度
     */
    watchKeyboardHeight() {
      this.windowStage.getMainWindowSync().on('keyboardHeightChange', (data: number) => {
        this.keyboardHeight = px2vp(data);
      });
    }

    /**
     * 设置全屏
     */
    setFullScreen() {
      this.windowStage.getMainWindowSync()
        .setWindowLayoutFullScreen(true)
        .then(() => {
          console.info('Succeeded in setting the window layout to full-screen mode.');
        })
        .catch((err: BusinessError) => {
          console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
        });
    }

    /**
     * 取消全屏
     */
    cancelFullScreen() {
      this.windowStage.getMainWindowSync()
        .setWindowLayoutFullScreen(false)
        .then(() => {
          console.info('Succeeded in setting the window layout to full-screen mode.');
        })
        .catch((err: BusinessError) => {
          console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
        });
    }

    /**
     * 隐藏头部状态栏
     */
    hideSystemTopStatusBar() {
      this.windowStage.getMainWindowSync()
        .setSpecificSystemBarEnabled('status', false)
        .then(() => {
          console.info('Succeeded in setting the status bar to be invisible.');
        })
        .catch((err: BusinessError) => {
          console.error(`Failed to set the status bar to be invisible. Code is ${err.code}, message is ${err.message}`);
        });
    }

    /**
     * 显示头部状态栏
     */
    showSystemTopStatusBar() {
      this.windowStage.getMainWindowSync()
        .setSpecificSystemBarEnabled('status', true)
        .then(() => {
          console.info('Succeeded in setting the status bar to be invisible.');
        })
        .catch((err: BusinessError) => {
          console.error(`Failed to set the status bar to be invisible. Code is ${err.code}, message is ${err.message}`);
        });
    }

    /**
     * 隐藏底部导航条
     */
    hideSystemBottomNavigationBar() {
      this.windowStage.getMainWindowSync()
        .setSpecificSystemBarEnabled('navigationIndicator', false)
        .then(() => {
          console.info('Succeeded in setting the navigation indicator to be invisible.');
        })
        .catch((err: BusinessError) => {
          console.error(`Failed to set the navigation indicator to be invisible. Code is ${err.code}, message is ${err.message}`);
        });
    }

    /**
     * 显示底部区域
     */
    showSystemBottomNavigationBar() {
      this.windowStage.getMainWindowSync()
        .setSpecificSystemBarEnabled('navigationIndicator', true)
        .then(() => {
          console.info('Succeeded in setting the navigation indicator to be invisible.');
        })
        .catch((err: BusinessError) => {
          console.error(`Failed to set the navigation indicator to be invisible. Code is ${err.code}, message is ${err.message}`);
        });
    }
  }

  /**
   * 避让区
   */
  class AvoidArea {
    topRect: Rect;
    bottomRect: Rect;

    constructor(topRect: Rect, bottomRect: Rect) {
      this.topRect = topRect
      this.bottomRect = bottomRect
    }
  }

  /**
   * 将矩形的px单位的数值转换为以vp为单位的数值
   * @param rect
   * @returns
   */
  function rect_px2vp(rect: Rect): Rect {
    return {
      left: px2vp(rect.left),
      top: px2vp(rect.top),
      width: px2vp(rect.width),
      height: px2vp(rect.height)
    } as Rect
  }
}
全部评论

相关推荐

06-11 19:44
已编辑
电子科技大学 算法工程师
感觉挺无语的。之前去官网提交的简历,过了近一个月,最近收到某米的大模型岗的面试通知,本来不是很信的(因为确实时间太久了),感觉放进去大概率kpi面,结果某米一天内发了两次面试预约邮件,我就相信确实是部门需要人,定了两天后的面试并全力准备来期望通过,想着就算没通过也能拿点意见不是嘛,就当学习了。结果到今天面试前40分钟,点进去面试链接被告知面试失效,官网的预约面试界面也无缘无故变成面试被取消,当时有点被气到了,人在无语时真的会笑一下😂😂。发邮件进行询问,被告知今天上午,之前的候选人接受offer了,岗位不需要人了,就把我的面试取消了,哈哈哈。我觉得如果没有保证说有名额的话,不需要去额外找冤大头或者备胎哈。退一步来说你们部门都有候选人到最后一步了还放别人去开始面试干嘛?😅再退一步来说,你们临时取消面试都不通知被试者吗?让别人耗着?有什么意思吗?个人感受:我发这些只是希望大家根据我的一点经历来避雷一些公司或部门。因为说实话这种浪费别人两三天的时间确实很ex。而且真的真的非常影响心情和自己原本的计划。比如今天,从上午我得知这件事,到下午一直有些感觉上的不舒服😇。而且原本的计划也被打乱,只能打碎牙往肚子里咽,怪自己倒霉咯。以后个人添加设备的时候应该不会考虑某米牌子的了😅,直接选择我华了
点赞 评论 收藏
分享
06-19 12:47
已编辑
门头沟学院 Java
整体流程比较快,早上十一点一面,下午两点半二面,一面两题简单手撕,二面无手撕一面:1.&nbsp;挑一个项目介绍?有什么有挑战的地方?2.&nbsp;之前都是做开发的工作,为什么选择测开?你认为测试开发岗主要工作是什么?3.&nbsp;了解哪些自动化测试工具?tps和qps的区别?4.&nbsp;你认为完整的测试流程是什么样的?5.&nbsp;如果你提交了一个bug,开发人员不认为这是一个bug,如何处理?6.&nbsp;过往生活中,有没有什么不太如意的经历?你是如何解决的?7.&nbsp;有没有关注过大模型相关的技术?8.&nbsp;接下来的工作规划?老家是福建的为什么选择北京?9.&nbsp;实验室项目整体节奏?是否会有压力?手撕:1.&nbsp;判断两个字符串是否有公共元素2.&nbsp;判断一个字符串是否有重复元素------------------------------------------------------------二面:1.&nbsp;老家是福建的,实习也好,工作也好,会考虑北京吗?(问就是会)2.&nbsp;详细介绍一下研究课题?是否有小论文的要求?3.&nbsp;在三亚海洋研究院以理论研究还是实践为主?4.&nbsp;为什么选择测试开发这个岗位?(还不是你们开发岗不约面~)对测试开发岗位的理解?5.&nbsp;如果让你去测试淘宝搜索框,你会怎么测试?6.&nbsp;万鹿公考是自己练手项目还是实际的项目,是否已经交付?你在这个项目中的职责?项目技术栈?7.&nbsp;项目开发过程中,有没有出现卡壳的情况?8.&nbsp;解析文档用的库还是自己实现的?9.&nbsp;实习过程中工作压力如何?时间很紧的话如何保证整体流程?10.&nbsp;下班之后的兴趣爱好?11.&nbsp;本硕阶段是否参加过比赛?12.&nbsp;你遇到的比较有挑战的事情?如何和团队其他人进行协作?13.&nbsp;项目快要上线了,开发人员修改bug的速度比较慢,你会怎么处理?14.&nbsp;项目开发过程中,如果研发人员不进行自测,导致很小的功能都会有bug,你会怎么处理?15.&nbsp;&nbsp;平常是如何学习新技术的?是否写过技术博客?16.&nbsp;自我评价优势和不足?性格上属于强势还是nice?17.&nbsp;实习过程中,有没有主动去做一些不是领导安排的工作?18.&nbsp;再次确认北京和福建离得远,为什么考虑北京呢?(问就是大城市机会多,朋友都在那边)能够什么时候开始实习?一周几天?反问:部门业务,面试结果什么时候出(好像说还要hr面?)----------------------------6.19更新&nbsp;口头offer了,等待审批
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务