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

相关推荐
光锥智能3 小时前
靠华为脱胎换骨,但赛力斯仍需要Plan B
华为
搞瓶可乐7 小时前
鸿蒙ArkUI实战之组件;Text组件,Image组件,Button组件,Span组件和TextInput组件的使用场景及使用方法
华为·harmonyos·鸿蒙系统·arkui·组件化开发·基础组件使用
九丘教育8 小时前
【仓颉 + 鸿蒙 + AI Agent】CangjieMagic框架(15):NaiveExecutor
人工智能·华为·harmonyos
搞瓶可乐12 小时前
鸿蒙ArkUI实战之TextArea组件、RichEditor组件、RichText组件、Search组件的使用
华为·harmonyos·arkui·搜索框·富文本组件·富文本输入框·鸿蒙原生api
孤寂码农_defector12 小时前
鸿蒙系统的 “成长烦恼“:生态突围与技术迭代的双重挑战
macos·华为·objective-c·cocoa·harmonyos
别说我什么都不会13 小时前
【仓颉三方库】 数据解析——TOML4CJ
harmonyos
鸿蒙布道师13 小时前
鸿蒙NEXT开发LRUCache缓存工具类(单例模式)(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
城中的雾15 小时前
HarmonyOS Next 编译之如何构建不同包名应用
harmonyos·arkts
别说我什么都不会15 小时前
【仓颉三方库】 数据解析—— jwt4cj
harmonyos
周胡杰16 小时前
鸿蒙-跨设备互通,设备互通提供跨设备的相机、扫描、图库访问能力,平板或2in1设备可以调用手机的相机、扫描、图库等功能。
数码相机·华为·自动化·电脑·harmonyos·鸿蒙·鸿蒙系统