鸿蒙OS开发之动画相关示例分享, 关于弹出倒计时动画的实战案例源码分享

ops/2024/10/21 9:24:02/

基础动画案例

@Entry
@Component
struct Index {@StatebtnWidth:number = 200 // 按钮的宽度@StatebtnHeight:number = 100 // 按钮的高度build() {Row(){Column(){Button("测试").width(this.btnWidth).height(this.btnHeight)// 按钮: 用来启动动画Button("动画开始").onClick(()=>{// 写动画animateTo({duration:1000},()=>{this.btnWidth=100this.btnHeight=50})})}.width("100%")}.height("100%")}
}

动画的播放次数

@Entry
@Component
struct Index {@StatebtnWidth:number = 200 // 按钮的宽度@StatebtnHeight:number = 100 // 按钮的高度build() {Row(){Column(){Button("测试").width(this.btnWidth).height(this.btnHeight)// 按钮: 用来启动动画Button("动画开始").onClick(()=>{// 写动画// iterations: -1 表示永久, 其他表示固定次数animateTo({duration:1000, iterations: 3},()=>{this.btnWidth=100this.btnHeight=50})})}.width("100%")}.height("100%")}
}

动画的播放模式

@Entry
@Component
struct Index {@StatebtnWidth:number = 200 // 按钮的宽度@StatebtnHeight:number = 100 // 按钮的高度build() {Row(){Column(){Button("测试").width(this.btnWidth).height(this.btnHeight)// 按钮: 用来启动动画Button("动画开始").onClick(()=>{// 写动画// iterations: -1 表示永久, 其他表示固定次数// playMode: Reverse 反向 Alternate反复animateTo({duration:1000, iterations: -1, playMode:PlayMode.Alternate},()=>{this.btnWidth=100this.btnHeight=50})})}.width("100%")}.height("100%")}
}

animation动画

@Entry
@Component
struct Index {@StatebtnWidth:number = 200 // 按钮的宽度@StatebtnHeight:number = 100 // 按钮的高度build() {Row(){Column(){Button("测试").width(this.btnWidth).height(this.btnHeight).animation({duration:1000,iterations: -1,playMode:PlayMode.Alternate})// 按钮: 用来启动动画Button("动画开始").onClick(()=>{this.btnWidth=100this.btnHeight=50})}.width("100%")}.height("100%")}
}

scale缩放动画

@Entry
@Component
struct Index {@StatebtnWidth:number = 200 // 按钮的宽度@StatebtnHeight:number = 100 // 按钮的高度@StatebtnScale:number = 1; // 缩放build() {Row(){Column(){Button("测试").width(this.btnWidth).height(this.btnHeight).scale({x: this.btnScale,y: this.btnScale,}).animation({duration:1000,iterations: -1,playMode:PlayMode.Alternate})// 按钮: 用来启动动画Button("动画开始").onClick(()=>{this.btnScale *= 2})}.width("100%")}.height("100%")}
}

显示隐藏动画

@Entry
@Component
struct Index {@Statemessage:string = "你好, 张大鹏!"@StateisShowMessage:boolean = truebuild() {Row(){Column(){// 固定高度的ColumnColumn(){if(this.isShowMessage){Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)}}.height(50)// 按钮, 控制Button("显示/隐藏").onClick(()=>{animateTo({duration:1000},()=>{this.isShowMessage=!this.isShowMessage})})}.width("100%")}.height("100%")}
}

弹出模态框

@Entry
@Component
struct Index {@StateisShowDialog:boolean = false// 模态框内容@BuildergetContent(){Column(){Text("测试").fontColor(Color.White)}.justifyContent(FlexAlign.Center).backgroundColor(Color.Blue).width("100%").height("100%")}build() {Row(){Button("显示/隐藏").onClick(()=>{this.isShowDialog=!this.isShowDialog})}.height("100%").bindContentCover($$this.isShowDialog, // 模态框弹出条件this.getContent, // 绑定模态框)}
}

弹出倒计时广告

@Entry
@Component
struct Index {@StateisShowDialog: boolean = false@StatetimerCount: number = 5 // 默认5秒倒计时关闭广告timer: number = -1 // 定时器// 开始倒计时beginTimerCount() {this.timer = setInterval(() => {if (this.timerCount === 0) {clearInterval(this.timer)this.timerCount = 5 // 重置计时器this.isShowDialog = false // 关闭模态框}this.timerCount--}, 1000)}// 生命周期方法: 页面消失之前aboutToDisappear(): void {clearInterval(this.timer) // 防止定时器没有及时清理}// 模态框内容@BuildergetContent() {Column() {// 右上角Row() {Text(`还剩${this.timerCount}`).fontColor(Color.White)}.width("100%").justifyContent(FlexAlign.End).padding(10)}.backgroundColor(Color.Blue).width("100%").height("100%")}build() {Row() {Button("显示").onClick(() => {this.isShowDialog = true // 显示模态框this.beginTimerCount() // 开始倒计时})}.height("100%").bindContentCover($$this.isShowDialog, // 模态框弹出条件this.getContent, // 绑定模态框{modalTransition: ModalTransition.NONE, // 取消模态框动画})}
}

在这里插入图片描述


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

相关文章

研究生三年概括

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、研一1.上学期2. 下学期 二、研二1.研二上2.研二下 三、研三1.研三上2.研三下 前言 不知道是谁说的了,人生的路很长,关键的就那么几…

Qt C++设计模式->享元模式

享元模式(Flyweight Pattern)是一种结构型设计模式,旨在通过共享相同对象来减少内存使用,尤其适合在大量重复对象的情况下。它通过将对象的可共享部分抽取出来,并在多个上下文中共享,从而避免对象的多次创建…

ESP32 Bluedroid 篇(1)—— ibeacon 广播

前言 前面我们已经了解了 ESP32 的 BLE 整体架构,现在我们开始实际学习一下Bluedroid 从机篇的广播和扫描。本文将会以 ble_ibeacon demo 为例子进行讲解,需要注意的一点是。ibeacon 分为两个部分,一个是作为广播者,一个是作为观…

Sharp.js:简单而又实用的图像处理库

前言 在现代Web开发中,图像处理是一个不可或缺的部分。 前端开发者经常需要处理图像,以确保它们在不同的设备和分辨率上都能保持良好的显示效果。 sharp.js是一个高性能的Node.js模块,它利用了libvips库,提供了快速且高效的图像…

ERROR [internal] load metadata for docker.io/library/openjdk:8

ERROR: failed to solve: DeadlineExceeded: DeadlineExceeded: DeadlineExceeded: openjdk:8: failed to do request: Head “https://registry-1.docker.io/v2/library/openjdk/manifests/8”: dial tcp 202.160.129.6:443: i/o timeout 在构建docker镜像时从docker.io/libr…

探究Spring的单例设计模式--单例Bean

Spring的单例设计模式 在Spring框架中,单例设计模式是一种常见且重要的设计模式,主要用于确保在应用程序的生命周期中仅创建一个特定的Bean实例 一、什么是单例设计模式? 单例设计模式是一种创建型设计模式,确保一个类只有一个…

Apollo Planning2.0决策规划算法代码详细解析 (3):PlanningComponent框架介绍

Apollo Planning 2.0的框架更新涉及多个方面,这些更新旨在提升自动驾驶系统的灵活性、可扩展性和性能。 以下是Apollo Planning 2.0 的框架图: 其中,Apollo的PlanningComponent在自动驾驶系统中扮演着至关重要的角色。其主要作用可以归纳为以…

Pinia从安装到使用

什么是Pinia 添加Pinia到vue项目 使用Pinia实现计数器案例 counter.js import {defineStore} from "pinia"; import {ref} from "vue";export const useCounterStore defineStore(coutner,()>{//定义数据(state)const count r…