【鸿蒙开发】闪屏页面练习

ops/2025/2/12 21:54:03/

1. 创建页面 Index.ets

@Entry
@Component
struct Index {build() {Column() {Text("首页").fontSize(50).fontWeight(FontWeight.Bold)}.width('100%').height('100%')}
}

2. 创建页面 SplashScreen.ets

@Entry
@Component
struct SplashScreen {@State message: string = 'SplashScreen'build() {Row() {Column() {Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)}.width('100%')}.height('100%')}
}

3. 创建store工具 StoreUtil.ets

import preferences from '@ohos.data.preferences'export class StoreUtil {constructor(private readonly context: Context, private readonly storeName: string) {}getStore() {return preferences.getPreferences(this.context, this.storeName)}async getData<T>(key): Promise<T> {const store = await this.getStore()const data = await store.get(key, "{}") as stringreturn JSON.parse(data) as T}async setData<T>(key: string, data: T): Promise<void> {const store = await this.getStore()await store.put(key, JSON.stringify(data))await store.flush()}async delData(key: string): Promise<void> {const store = await this.getStore()await store.delete(key)await store.flush()}
}

4. 创建类型 typs/SplashScreen.ets

export interface ISplashScreen {show: booleanduration: numberimage: string | Resource
}

5. 修改 EntryAbility

  • 修改 EntryAbility.ts 后缀为 EntryAbility.ets
  • 创建 userStore
  • 模拟获取闪屏数据
  • 从 userStore 获取闪屏数据
  • 加载首页或者闪屏页
  async onWindowStageCreate(windowStage: window.WindowStage) {// Main window is created, set main page for this abilityhilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');// 用户仓库const userStore = new StoreUtil(this.context, "userStore",)// 模拟获取闪屏数据await new Promise(resolve => {setTimeout(async () => {const splashScreen: ISplashScreen = {show: true,duration: 10,image: $r("app.media.splash_screen")}await userStore.setData("userSplashScreen", splashScreen)resolve(splashScreen)}, 2000)})// 从仓库获取闪屏数据const splashScreen = await userStore.getData<ISplashScreen>("userSplashScreen")if (splashScreen.show) {// 加载闪屏页面windowStage.loadContent('pages/SplashScreen', (err, data) => {if (err.code) {hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');return;}hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');});} else {// 加载首页windowStage.loadContent('pages/Index', (err, data) => {if (err.code) {hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');return;}hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');});}}

6. 修改闪屏页面

修改 SplashScreen.ets

import router from '@ohos.router'
import { ISplashScreen } from '../types/SplashScreen'
import { StoreUtil } from '../utils/StoreUtil'@Entry
@Component
struct SplashScreen {@State timer: number = -1@State splashScreenObj: ISplashScreen = { show: true, duration: 10, image: '' }userStore: StoreUtil = new StoreUtil(getContext(this), 'userStore')aboutToAppear() {this.getData()}aboutToDisappear() {clearInterval(this.timer)}async getData() {// 获取闪屏数据const data = await this.userStore.getData<ISplashScreen>('userSplashScreen')this.splashScreenObj.duration = data?.duration ?? 10this.splashScreenObj.image = data?.image ?? $r('app.media.splash_screen')// 倒计时this.timer = setInterval(() => {if (this.splashScreenObj.duration === 0) {clearInterval(this.timer)router.replaceUrl({ url: 'pages/Index' })return}this.splashScreenObj.duration--}, 1000)}build() {Stack({ alignContent: Alignment.TopEnd }) {Image(this.splashScreenObj.image).width('100%').height('100%')Row() {Text(`${this.splashScreenObj.duration}秒后跳过`).padding({ left: 10, right: 10 }).margin({ right: 20, top: 20 }).height(30).fontSize(14).borderRadius(15).backgroundColor("#ccc").textAlign(TextAlign.Center)Text(`跳过`).padding({ left: 10, right: 10 }).margin({ right: 20, top: 20 }).height(30).fontSize(14).borderRadius(15).backgroundColor("#ccc").textAlign(TextAlign.Center).onClick(() => {router.replaceUrl({ url: 'pages/Index' })})}}.width('100%').height('100%')}
}


http://www.ppmy.cn/ops/20248.html

相关文章

MySQL商城数据表(20-29)

20快递表 DROP TABLE IF EXISTS xuge_express; CREATE TABLE xuge_express (expressId int(11) NOT NULL AUTO_INCREMENT COMMENT 自增id,expressName varchar(50) NOT NULL COMMENT 快递名称,-dataFlag tinyint(4) NOT NULL DEFAULT 1 COMMENT 有效标记&#xff08;1 &#x…

windows SDK编程 --- 消息之键盘消息(4)

前置知识 一、 键盘消息 在Windows操作系统中&#xff0c;键盘消息是用来通知应用程序有关键盘输入事件的一种机制。当用户在键盘上进行操作&#xff0c;比如按键或释放键时&#xff0c;Windows会生成相应的消息并发送给处理这些输入的应用程序。这些消息对于开发图形用户界面…

java算法 递归运算优化(备忘录模式)

一、递归是很常见的一种运算&#xff0c;有明显特征特征的场景一般使用递归比较合适。其一&#xff0c;相邻的数据存在一定的逻辑关系&#xff0c;其二、必须存在出口的值&#xff0c;如第一个元素或者最后元素的值能轻松计算出来。 二、递归和备忘录示例 1、题目&#xff1a;…

Redis篇:缓存雪崩及解决方案

1.何为缓存雪崩 缓存雪崩是指在同一时段大量的缓存key同时失效或者Redis服务宕机&#xff0c;导致大量请求到达数据库&#xff0c;带来巨大压力。 2.缓存雪崩的解决方案 解决方案&#xff1a; 给不同的Key的TTL添加随机值 利用Redis集群提高服务的可用性 给缓存业务添加降级…

Golang函数和包

文章目录 函数基本语法参数传递方式返回值特点可变参数函数类型匿名函数defer机制闭包 包基本概念包的使用init函数 函数 基本语法 基本语法 Go中函数的基本语法如下&#xff1a; 使用案例如下&#xff1a; package mainimport "fmt"// 函数定义 func Factorial(nu…

(python)动态规划

前言 曾经有一位叫做小明的年轻人,他生活在一个被困在连绵不断的山脉中的村庄里。这座村庄每年都会受到洪水的威胁,而村民们只能通过一条崎岖而危险的小路逃离洪水的侵袭。小明决定解决这个问题。他花了很长时间研究了地形图和洪水的模式,最终他发现了一种方法:他可以在山脚…

骑砍2霸主MOD开发(8)-action_sets.xml骨骼动画

一.action_sets.xml 1.文件目录:Modules\Native\ModuleData\action_sets.xml 2.action_set(骨骼动画animation) action1 action2 action3 3.通过在action_set中配置skeleton,人,马匹,牛,羊等骨架 二.使用编辑器修改动作 三.骨架&骨骼&骨骼动画 1.骨架skeleton Skele…

Zabbix 安装部署说明文档

Zabbix是一个开源的网络监控和管理系统&#xff0c;其架构设计用于提供企业级的监控解决方案。以下是Zabbix的主要组件&#xff1a; 1.Zabbix Server&#xff1a;这是Zabbix系统的核心组件&#xff0c;负责接收Agent程序报告的系统可用性、系统完整性和统计数据。Zabbix Serve…