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

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

基础动画案例

@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%")
  }
}

动画的播放次数

@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%")
  }
}

动画的播放模式

@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动画

@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缩放动画

@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%")
  }
}

显示隐藏动画

@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%")
  }
}

弹出模态框

@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, // 绑定模态框
    )
  }
}

弹出倒计时广告

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

在这里插入图片描述


http://www.kler.cn/news/323661.html

相关文章:

  • netty之Future和Promise
  • leetcode946. 验证栈序列
  • ARM GNU工具链
  • 赵长鹏今日获释,下一步会做什么?币安透露2024年加密货币牛市的投资策略!
  • 【数据结构-栈】力扣71. 简化路径
  • react-native如何一个项目生成两个安装包
  • 什么是Kafka?
  • 利用 Llama-3.1-Nemotron-51B 推进精度-效率前沿的发展
  • PlayerPerfs-不同平台的存储位置
  • 十一假期地区人流量出行大数据分析:技术驱动下的深度洞察
  • [Excel VBA办公]如何使用VBA批量删除空行
  • 基于微信小程序的网上商城+ssm(lw+演示+源码+运行)
  • 基于Hive和Hadoop的病例分析系统
  • Web会话跟踪+代码分析
  • 在C#中实现WebSocket的单聊和分频道聊天
  • Python 绘图艺术:解锁数据故事的三把密钥
  • AJAX(简介以及一些用法)
  • SQL 基础语法
  • Go conc库学习与使用
  • Ubuntu 16.04安装填坑记录
  • 夜间红外图宠物检测系统源码分享
  • 视频美颜SDK与直播美颜工具API是什么?计算机视觉技术详解
  • 履带机器人运动模型分析
  • 如何选择游戏高防服务器,有什么需要注意的点?
  • 一体化杀猪厂污水处理设备特点
  • 数据科学的核心工具箱:全面解析pandas、matplotlib.pyplot与scipy.stats在复杂数据分析流程中的应用
  • Maya学习笔记:项目设置和快捷键
  • 【已解决】【Hadoop】找到java环境路径
  • C++ —— 关于list
  • 数据结构:队列及其应用