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

基础动画案例

js 复制代码
@Entry
@Component
struct Index {
  @State
  btnWidth:number = 200 // 按钮的宽度
  @State
  btnHeight:number = 100 // 按钮的高度

  build() {
   Row(){
     Column(){
       Button("测试")
         .width(this.btnWidth)
         .height(this.btnHeight)

       // 按钮: 用来启动动画
       Button("动画开始")
         .onClick(()=>{
           // 写动画
           animateTo({duration:1000},()=>{
             this.btnWidth=100
             this.btnHeight=50
           })
         })
     }.width("100%")
   }.height("100%")
  }
}

动画的播放次数

js 复制代码
@Entry
@Component
struct Index {
  @State
  btnWidth:number = 200 // 按钮的宽度
  @State
  btnHeight:number = 100 // 按钮的高度

  build() {
   Row(){
     Column(){
       Button("测试")
         .width(this.btnWidth)
         .height(this.btnHeight)

       // 按钮: 用来启动动画
       Button("动画开始")
         .onClick(()=>{
           // 写动画
           // iterations: -1 表示永久, 其他表示固定次数
           animateTo({duration:1000, iterations: 3},()=>{
             this.btnWidth=100
             this.btnHeight=50
           })
         })
     }.width("100%")
   }.height("100%")
  }
}

动画的播放模式

js 复制代码
@Entry
@Component
struct Index {
  @State
  btnWidth:number = 200 // 按钮的宽度
  @State
  btnHeight: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=100
             this.btnHeight=50
           })
         })
     }.width("100%")
   }.height("100%")
  }
}

animation动画

js 复制代码
@Entry
@Component
struct Index {
  @State
  btnWidth:number = 200 // 按钮的宽度
  @State
  btnHeight:number = 100 // 按钮的高度

  build() {
   Row(){
     Column(){
       Button("测试")
         .width(this.btnWidth)
         .height(this.btnHeight)
         .animation({
           duration:1000,
           iterations: -1,
           playMode:PlayMode.Alternate
         })

       // 按钮: 用来启动动画
       Button("动画开始")
         .onClick(()=>{
             this.btnWidth=100
             this.btnHeight=50
         })
     }.width("100%")
   }.height("100%")
  }
}

scale缩放动画

js 复制代码
@Entry
@Component
struct Index {
  @State
  btnWidth:number = 200 // 按钮的宽度
  @State
  btnHeight:number = 100 // 按钮的高度
  @State
  btnScale: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%")
  }
}

显示隐藏动画

js 复制代码
@Entry
@Component
struct Index {
  @State
  message:string = "你好, 张大鹏!"
  @State
  isShowMessage:boolean = true

  build() {
   Row(){
     Column(){
       // 固定高度的Column
       Column(){
         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%")
  }
}

弹出模态框

js 复制代码
@Entry
@Component
struct Index {
  @State
  isShowDialog:boolean = false

  // 模态框内容
  @Builder
  getContent(){
    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, // 绑定模态框
    )
  }
}

弹出倒计时广告

js 复制代码
@Entry
@Component
struct Index {
  @State
  isShowDialog: boolean = false
  @State
  timerCount: 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) // 防止定时器没有及时清理
  }

  // 模态框内容
  @Builder
  getContent() {
    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, // 取消模态框动画
      }
    )
  }
}
相关推荐
上海张律师3 小时前
鸿蒙ArkTS+ArkUI仿微信消息列表页制作
harmonyos
fydw_7153 小时前
生产环境中安装和配置 Nginx 以部署 Flask 应用的详细指南
运维·nginx·flask
xzh3 小时前
问题:Nginx client_body_temp_path 文件会删除吗,删除时机?
nginx·架构
dessler5 小时前
代理服务器-LVS的3种模式与调度算法
运维·服务器·网络·算法·nginx·tomcat·lvs
Humbunklung8 小时前
关于华为仓颉编程语言
华为·cangjie
2501_911121239 小时前
Nginx+Tomcat 负载均衡群集
nginx·tomcat·负载均衡
王二蛋与他的张大花10 小时前
HarmonyOS运动开发:如何用mpchart绘制运动配速图表
harmonyos
程序员小刘10 小时前
【HarmonyOS 5】生活与服务开发实践详解以及服务卡片案例
华为·生活·harmonyos
小草帽学编程14 小时前
鸿蒙Next开发真机调试签名申请流程
android·华为·harmonyos
陈奕昆14 小时前
5.2 HarmonyOS NEXT应用性能诊断与优化:工具链、启动速度与功耗管理实战
华为·harmonyos