自定义HarmonyOS启动页组件

启动页作为应用程序首次出现的页面,该页面提供一些预加载数据的提前获取,防止应用程序出现白屏等异常,如是否第一次访问应用程序并开启应用欢迎页;判断用户登录信息进行页面跳转;消息信息懒加载等。

常见启动页参数如下表所示:

属性类型描述必填
timernumber倒计时时长,默认3秒Y
isLogoboolean显示图片类型。
false:常规图,默认;
true:logo图
N
backgroundImgResourceStr显示图片地址N
companyNamestring企业名称N
mfontColorResourceColor企业名称字体颜色N

常见启动页方法如下表所示:

方法类型描述必填
skipvoid跳转方法Y

封装启动页参数类代码如下所示:

export class Splash {
  // 倒计时时长
  timer: number;
  // 显示Logo
  isLogo?: boolean = false;
  // 页面显示图片
  backgroundImg?: ResourceStr;
  // 企业名称
  companyName?: string;
  // 企业名称字体颜色
  mFontColor?: ResourceColor;

  constructor(timer: number, isLogo?: boolean, backgroundImg?: ResourceStr,
              companyName?: string, mFontColor?: ResourceColor) {
    this.timer = timer;
    this.isLogo = isLogo;
    this.backgroundImg = backgroundImg;
    this.companyName = companyName;
    this.mFontColor = mFontColor;
  }
}

自定义启动页组件代码如下所示:

@Component
export struct SplashPage {

  @State mSplash: Splash = new Splash(3);

  // 跳转方法
  skip: () => void;

  build() {
    // 底部企业名称显示堆叠组件
    Stack({ alignContent: Alignment.Bottom }) {
      // 图片和倒计时跳转页面堆叠组件
      Stack({ alignContent: Alignment.TopEnd }) {
        if (this.mSplash.isLogo) {
          Image(this.mSplash.backgroundImg).objectFit(ImageFit.None)
        }
        Button(`跳过 | ${this.mSplash.timer} s`, { type: ButtonType.Normal })
          .height(42)
          .padding({ left: 16, right: 16 })
          .margin({ top: 16, right: 16 })
          .fontSize(16).fontColor(Color.White)
          .backgroundColor(Color.Gray)
          .borderRadius(8)
          .onClick(() => {
            this.skip();
          })
      }
      .backgroundImage(this.mSplash.isLogo ? null : this.mSplash.backgroundImg)
      .backgroundImageSize(this.mSplash.isLogo ? null : { width: '100%', height: '100%' })
      .width('100%').height('100%')
      if (this.mSplash.companyName) {
        Text(this.mSplash.companyName)
          .width('100%').height(54)
          .fontColor(this.mSplash.mFontColor)
          .fontSize(14).fontWeight(FontWeight.Bold)
          .textAlign(TextAlign.Center)
      }
    }
    .width('100%').height('100%')
  }

  aboutToAppear() {
    // 倒计时处理
    let skipWait = setInterval(() => {
      this.mSplash.timer--;
      if (this.mSplash.timer === 0) {
        clearInterval(skipWait);
        this.skip();
      }
    }, 1000)
  }
}

自定义组件定义完成后,还需要在模块的index.ets中将组件导出,代码如下所示:

export { Splash, SplashPage } from './src/main/ets/components/splashpage/SplashPage';

在entry模块引入自定义模块teui,打开entry目录下的package.json并在dependencies依赖列中加入如下代码:

"@tetcl/teui": "file:../teui"

注:其中"@tetcl/teui"中"tetcl/teui"需要和自定义模块teui中package.json中name属性一致。若提交到npm中心仓可直接使用"@tetcl/teui": "版本号"方式引入。引入完成后需要执行编辑器上的Sync now或者npm install进行下载同步。

在具体的页面中导入自定义启动页组件代码如下所示:

import { Splash as SplashObj, SplashPage } from '@tetcl/teui'
import router from '@ohos.router';

注:为了和页面名称不冲突,对Splash作别名处理。

在页面中引入自定义组件SplashPage并填写相关属性值及跳转方法,代码如下所示:

@Entry
@Component
struct Splash {

  // 跳转到Index页面
  onSkip() {
    router.replaceUrl({
      url: 'pages/Index'
    })
  }

  build() {
    Row() {
      SplashPage({ mSplash: new SplashObj(5, true, $r('app.media.icon'),
        'xxxx有限公司', 0x666666), skip: this.onSkip})
      // 常规图
      // SplashPage({ mSplash: new SplashObj(5, false, $r('app.media.default_bg'), 
      //  'xxxx有限公司', 0xF5F5F5), skip: this.onSkip})
    }
    .height('100%')
  }
}

预览效果如下图所示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坚果派·白晓明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值