Skip to content

HarmonyOS 实现自定义启动页

要在 HarmonyOS 中实现自定义启动页,需要在项目中 AbilityonWindowStageCreateonWindowStageRestore 方法中获取应用主窗口, 并通过调用窗口强制全屏布局接口 setWindowLayoutFullScreen 将窗口设置为全屏。

typescript
/**
 * 将窗口设置为全屏
 * @param windowStage
 */
setFullSize(windowStage: window.WindowStage) {
    let windowClass: window.Window = windowStage.getMainWindowSync(); // 获取应用主窗口
    // 设置窗口全屏
    let isLayoutFullScreen = true;
    windowClass.setWindowLayoutFullScreen(isLayoutFullScreen)
        .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));
        });
}

新建启动页 Splash.ets ,在该页面中的 aboutToAppear 生命周期钩子中使用 timer 定时器,实现延时跳转功能。最后在组件结构销毁之前的 aboutToDisappear 生命周期钩子中销毁 timer 定时器。

typescript
@State countdown: number = 5; // 倒计时,默认 5 秒
readonly DURATION: number = 1000; // 倒计时间隔
private timer: number = 0; // 定时器

aboutToAppear(): void {
  this.startTiming();
}

/**
 * 开始倒计时
 */
startTiming() {
  // 设置时间间隔
  this.timer = setInterval(() => {
    this.countdown--;
    if (this.countdown === 0) {
      this.clearTiming();
      this.jumpToMainPage();
    }
  }, this.DURATION);
}

/**
 * 清除定时器
 */
clearTiming() {
  if (this.timer !== null) {
    clearInterval(this.timer);
    this.timer = 0;
  }
}

/**
 * 跳转到主页
 */
jumpToMainPage() {
  this.clearTiming();
  router.replaceUrl({
    url: 'pages/Index'
  });
}

/**
 * 组件结构销毁之前时
 */
aboutToDisappear() {
  // 清除定时器
  this.clearTiming();
}

运行效果如下:

24071801.gif

编程洪同学服务平台是一个广泛收集编程相关内容和资源,旨在满足编程爱好者和专业开发人员的需求的网站。无论您是初学者还是经验丰富的开发者,都可以在这里找到有用的信息和资料,我们将助您提升编程技能和知识。
专业开发
高端定制
售后无忧
站内资源均为本站制作或收集于互联网等平台,如有侵权,请第一时间联系本站,敬请谅解!本站资源仅限于学习与参考,严禁用于各种非法活动,否则后果自行负责,本站概不承担!