当前位置: 首页 > news >正文

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

基础动画案例

@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, // 取消模态框动画})}
}

在这里插入图片描述

相关文章:

  • AI大模型教程 Prompt提示词工程 AI原生应用开发零基础入门到实战【2024超细超全,建议收藏】
  • Maven重点学习笔记(包入门 2万字)
  • 搜索引擎onesearch3实现解释和升级到Elasticsearch v8系列(二)-索引
  • 软考中级网络工程师选择题
  • 华为OD机试真题------分糖果
  • Docker配置代理解决pull超时问题
  • 大数据-146 Apache Kudu 安装运行 Dockerfile 模拟集群 启动测试
  • PSS-sdy_opengl_sdd
  • 【机器学习(九)】分类和回归任务-多层感知机(Multilayer Perceptron,MLP)算法-Sentosa_DSML社区版
  • 低代码革命:加速云原生时代的端到端产品创新
  • 使用Hutool-poi封装Apache POI进行Excel的上传与下载
  • 将图片资源保存到服务器的盘符中
  • FGPA实验——触摸按键
  • 3D 模型GLTF、GLB格式文件介绍使用;FBX格式
  • Linux网络之UDP与TCP协议详解
  • ➹使用webpack配置多页面应用(MPA)
  • Elasticsearch 参考指南(升级前重新索引)
  • leetcode-27. Remove Element
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • Shadow DOM 内部构造及如何构建独立组件
  • Spring核心 Bean的高级装配
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • 简单基于spring的redis配置(单机和集群模式)
  • 类orAPI - 收藏集 - 掘金
  • 实战|智能家居行业移动应用性能分析
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 事件委托的小应用
  • 详解NodeJs流之一
  • 一起参Ember.js讨论、问答社区。
  • 译自由幺半群
  • 带你开发类似Pokemon Go的AR游戏
  • ​【经验分享】微机原理、指令判断、判断指令是否正确判断指令是否正确​
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • # 深度解析 Socket 与 WebSocket:原理、区别与应用
  • # 数仓建模:如何构建主题宽表模型?
  • ###C语言程序设计-----C语言学习(6)#
  • #Datawhale AI夏令营第4期#AIGC方向 文生图 Task2
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • #免费 苹果M系芯片Macbook电脑MacOS使用Bash脚本写入(读写)NTFS硬盘教程
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别
  • (二)springcloud实战之config配置中心
  • (六)c52学习之旅-独立按键
  • (每日一问)基础知识:堆与栈的区别
  • (十七)devops持续集成开发——使用jenkins流水线pipeline方式发布一个微服务项目
  • ./configure,make,make install的作用
  • .bat批处理(六):替换字符串中匹配的子串
  • .NET 5.0正式发布,有什么功能特性(翻译)
  • .NET Core WebAPI中使用Log4net 日志级别分类并记录到数据库
  • .NET Core 版本不支持的问题
  • .NET Remoting学习笔记(三)信道
  • .NET 的程序集加载上下文
  • .Net 高效开发之不可错过的实用工具
  • .NET程序集编辑器/调试器 dnSpy 使用介绍