华为鸿蒙应用--欢迎页SplashPage+倒计时跳过(自适应手机和平板)-ArkTs

鸿蒙ArkTS 开发欢迎页SplashPage+倒计时跳过,可自适应平板和手机

一、SplashPage.ts
复制代码
import { BreakpointSystem, BreakPointType, Logger, PageConstants, StyleConstants } from '@ohos/common';
import router from '@ohos.router';

@Entry
@Component
struct SplashPage {
  @StorageProp('currentBreakpoint') currentBreakpoint: string = 'sm';
  private breakpointSystem = new BreakpointSystem();
  private intervalId: number = 0;
  @State  countdownTime: number = PageConstants.DELAY_TIME;
  private splash: Resource[] = [$r('app.media.img_splash1'), $r('app.media.img_splash2'), $r('app.media.img_splash3')];

  // 获取min到max的随机正整数
  getRandom(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min)
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) {
      Button(this.countdownTime + "秒 跳过", { type: ButtonType.Capsule, stateEffect: true })
        .alignSelf(ItemAlign.End)
        .margin({ top: $r('app.float.vp_thirty_two'), right: $r('app.float.vp_sixteen') })
        .onClick(() => {
          this.routerMain();
        })
      Column() {
        Image(this.splash[this.getRandom(0, 2)])  // 每次进入随机展示一张欢迎图片
          .width(new BreakPointType({
            sm: $r('app.float.splash_image_size'),
            md: $r('app.float.splash_image_size'),
            lg: $r('app.float.splash_image_size_lg')  // 手机和平板使用不同的图片宽度
          }).getValue(this.currentBreakpoint))
          .aspectRatio(PageConstants.IMAGE_ASPECT_RATIO)
          .objectFit(ImageFit.Contain)
      }
      .justifyContent(FlexAlign.Start)
      .alignItems(HorizontalAlign.Center)
      .flexGrow(StyleConstants.FLEX_GROW)
      .margin({ top: new BreakPointType({
        sm: $r('app.float.vp_hundred'),
        md: $r('app.float.vp_hundred'),
        lg: $r('app.float.vp_fifty')   // 手机和平板不同的margin
      }).getValue(this.currentBreakpoint) })

      Text($r('app.string.splash_desc'))
        .fontColor($r('app.color.color_797979'))
        .fontSize($r('app.float.small_font_size'))
        .margin({ top: $r('app.float.vp_one'), bottom: $r('app.float.vp_one') })

      Text($r('app.string.splash_filings'))
        .fontColor($r('app.color.color_797979'))
        .fontSize($r('app.float.small_font_size'))
        .margin({ top: $r('app.float.vp_one'), bottom: $r('app.float.empty_image_size') })
    }
    .height(StyleConstants.FULL_HEIGHT)
    .width(StyleConstants.FULL_WIDTH)
    .backgroundColor($r('app.color.page_background'))
  }
  // 倒计时
  countdown = () => {
    this.countdownTime -= 1;
    if (this.countdownTime == 0) {
      clearInterval(this.intervalId)
      this.routerMain();
    }
  }

  // 跳转首页
  routerMain = () => {
    router.replaceUrl({ url: PageConstants.MAIN_PAGE_URL })
      .catch((err: Error) => {
        Logger.error(JSON.stringify(err));
      })
  }

  aboutToAppear() {
    this.breakpointSystem.register();
    this.intervalId = setInterval(this.countdown, 1000);
  }

  aboutToDisappear() {
    this.breakpointSystem.unregister();
    clearInterval(this.intervalId);
  }
}

工具:BreakpointSystem, BreakPointType, Logger, PageConstants, StyleConstants等工具见:华为鸿蒙应用--底部导航栏Tabs(自适应手机和平板)-CSDN博客

相关推荐
风满城335 小时前
【鸿蒙原生应用开发实战】第五篇:项目总结——ArkTS 最佳实践与从 MVP 到生产的升级之路
华为·harmonyos
木咺吟5 小时前
鸿蒙原生应用实战(五):路由导航与工程优化 — 从开发到上线的完整流程
华为·harmonyos
风满城335 小时前
【鸿蒙原生应用开发实战】第三篇:表单录入与详情展示——AddPetPage + PetDetailPage 完整实现
华为·harmonyos
风满城336 小时前
【鸿蒙原生应用开发实战】第一篇:从零搭建“萌宠日记“项目——Stage模型与工程架构解析
华为·harmonyos
charlee446 小时前
Unity项目适配华为鸿蒙系统的原生库加载问题排查与解决
华为·unity3d·鸿蒙·cmake·c/c++·relro
狼哥16866 小时前
《新闻资讯》二、公共能力层模块实现指南
ui·华为·harmonyos
Ww.xh7 小时前
启用Hypervisor解决模拟器问题
华为·harmonyos
金启攻8 小时前
【鸿蒙原生应用实战】第二篇:装备库页面——分类筛选与数据驱动UI
harmonyos
木咺吟9 小时前
鸿蒙原生应用实战(四):愿望单与个人统计 — 数据聚合与可视化
华为·harmonyos
木咺吟10 小时前
鸿蒙原生应用实战(二):游戏库列表与筛选排序 — 卡片式UI设计
harmonyos