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

news/2024/9/24 7:22:08/

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/news/1440975.html

相关文章

使用 Python 和 DirectShow 从相机捕获图像

在 Python 中使用 OpenCV 是视觉应用程序原型的一个非常好的解决方案,它允许您快速起草和测试算法。处理从文件中读取的图像非常容易,如果要处理从相机捕获的图像,则不那么容易。OpenCV 提供了一些基本方法来访问链接到 PC 的相机(通过对象),但大多数时候,即使对于简单的…

阿里云安装Mysql

阿里云安装Mysql 1、下载mysql包&#xff0c;并解压包2、继续解压文件3、拷贝到安装目录4、创建mysql组和用户5、创建mysql数据目录&#xff08;根目录&#xff09;6、赋予权限7、加入路径8、配置文件顺序&#xff1a;9、初始化mysql10、启动mysql&#xff08;结尾加"&…

【JAVA】一文掌握Java并发编程

Java 开发中&#xff0c;并发编程属于相当重要的一个知识点&#xff0c;可以说&#xff0c;Java 的并发能力&#xff0c;是成就今日 Java 地位的因素之一。Java 的并发编程由浅入深实质上是包含 Java&#xff08;API&#xff09;层、JVM&#xff08;虚拟机&#xff09;层、内核…

28.Gateway-网关过滤器

GatewayFilter是网关中提供的一种过滤器&#xff0c;可以多进入网关的请求和微服务返回的响应做处理。 GatewayFilter(当前路由过滤器&#xff0c;DefaultFilter) spring中提供了31种不同的路由过滤器工厂。 filters针对部分路由的过滤器。 default-filters针对所有路由的默认…

OpenCV如何使用分水岭算法进行图像分割

OpenCV 使用分水岭算法进行图像分割的基本步骤如下&#xff1a; 加载图像&#xff1a;首先&#xff0c;你需要加载你要进行分割的图像。灰度化&#xff1a;将彩色图像转换为灰度图像&#xff0c;因为分水岭算法通常在灰度图像上操作。预处理&#xff1a;这一步可能包括滤波&am…

RTT学习 MQTT

MQTT背景应用 MQTT是机器对机器&#xff08;M2M&#xff09;/物联网&#xff08;IoT&#xff09;连接协议&#xff0c;它是专为受限设备和低带宽、高延迟或不可靠的网络而设计的&#xff0c;是一种基于发布/订阅&#xff08;publish/subscribe&#xff09;模式的“轻量级”通讯…

一主双从redis+哨兵模式以及通过Ansible+Shell快速部署redis哨兵模式

目录 一.主redis配置 1.解压并进行编译安装 2.为redis创建软链接和service管理方便启动 3.修改redis的配置文件 二.配置备redis 三.主redis上查看集群状态 1.主redis登录上去查看状态并且创建数据用于验证同步性 2.备redis查看数据是否同步过来 四.模拟测试 1.主redi…

Python数据分析实验二:Python数据预处理

目录 一、实验目的与要求二、实验任务三、主要程序清单和运行结果&#xff08;一&#xff09;对chipotle.csv文件的销售数据进行分析&#xff08;二&#xff09;对描述泰坦尼克号成员的信息进行可视化和相关分析 四、实验体会 一、实验目的与要求 1、目的&#xff1a;   掌握…