华为鸿蒙应用--欢迎页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博客

相关推荐
lilian23316 小时前
Harmony os 技术实战|拼豆制图09:把 50 张图库素材做成可复跑的生产管线
华为·harmonyos
chjif18 小时前
Android 设备管控开发实战:企业受控终端的 DNS 域名策略实现
android·华为·harmonyos
OH_TPC19 小时前
【鸿蒙优选三方库】@ohos/grpc:在 HarmonyOS 上像调本地方法一样调远程服务
华为·harmonyos·鸿蒙
达子66619 小时前
第30章_综合案例:HarmonyOs开发图解之 俄罗斯方块游戏
游戏·华为·harmonyos
云端漫步198719 小时前
HarmonyOS NEXT AI 应用开发总结:30 篇之旅
人工智能·华为·harmonyos
math_hongfan20 小时前
鸿蒙Agent高级技能自定义开发:自定义意图服务/能力注册/Agent技能编排/服务插件开发实战
android·学习·华为·harmonyos·鸿蒙
math_hongfan20 小时前
鸿蒙AI权限与隐私保护高级:智能数据脱敏/端侧处理优先/数据最小化采集/隐私计算技术栈
人工智能·学习·华为·harmonyos·鸿蒙
云端漫步19871 天前
HarmonyOS NEXT AI 智能生活助手:项目打包与发布
人工智能·华为·生活·harmonyos
less_121381 天前
HarmonyOS WPS Open SDK:关闭回传 URI-FD 与沙箱 filePath
华为·harmonyos·wps
math_hongfan1 天前
鸿蒙多模态AI交互高级:图文+语音+手势融合交互/多模态大模型端侧适配/跨模态检索高阶实战
人工智能·学习·华为·交互·语音识别·harmonyos·鸿蒙