华为 HarmonyOS NEXT 原生应用开发: 动画的基础使用(属性、显示、专场)动画

2024年11月5日 LiuJinTao

文章目录

鸿蒙中动画的使用

一、属性动画 - animation

属性动画代码示例
ts 复制代码
/**
 * 属性动画的演示
 */
@Entry
@Component
struct Index {
  @State selfWidth: number = 20
  @State setId: number = 0
  build() {
    Column() {
      Text("HarmonyOS NEXT")
        .fontWeight(FontWeight.Bold)
        .fontSize(this.selfWidth)
        .textAlign(TextAlign.Center)
        .animation({
          // 动画事件
          duration: 900,
          // 动画曲线
          // curve: Curve.Linear,
          curve: "cubic-bezier(1.00, -0.18, 1.00, -0.33)",
          // 延迟时间
          delay: 500,
          // 执行次数  (-1 无限次)
          iterations: 2,
          // 播放模式
          playMode: PlayMode.Alternate
        })

      Button("放大")
        .onClick(() => {
          // 这里通过定时器实现动画效果(20毫秒动一下,产生视觉效果!)
          this.setId = setInterval(() => {
            if (this.selfWidth < 50) {
              this.selfWidth++
            } else {
              clearInterval(this.setId)  //  关闭定时器
            }
          }, 30)
        })
      Button("缩小")
        .onClick(() => {
          // 当 animation 发现该组件属性发生变化,就会执行 animation属性动画 (这里我们改变字体大小)
          this.selfWidth = 20
        })
    }
    .height('100%')
    .width('100%')
    .justifyContent(FlexAlign.Center)
  }
}

二、显示动画 - AnimateTo

ts 复制代码
/**
 *  显示动画的演示
 */
@Entry
@Component
struct Index {
  @State selfWidth: number = 20
  @State setId: number = 0
  build() {
    Column() {
      Text("HarmonyOS NEXT")
        .fontWeight(FontWeight.Bold)
        .fontSize(this.selfWidth)
        .textAlign(TextAlign.Center)

      Button("放大")
        .onClick(() => {
          // 这里通过定时器实现动画效果(20毫秒动一下,产生视觉效果!)
          this.setId = setInterval(() => {
            if (this.selfWidth < 50) {
              this.selfWidth++
            } else {
              clearInterval(this.setId)  //  关闭定时器
            }
          }, 30)
        })
      Button("缩小")
        .onClick(() => {
          animateTo({
            // 这里可以写  animation 中的所有配置
            duration: 900,
          } , () => {
            // 事件触发,这里执行的事件逻辑都会通过动画形式呈现
            //  和 animation比较,这个比较专一,控制字体大小就只会呈现字体大小动画
            this.selfWidth = 20
          })
        })
    }
    .height('100%')
    .width('100%')
    .justifyContent(FlexAlign.Center)
  }
}

三、专场动画

  • Index
ts 复制代码
import { router } from '@kit.ArkUI'

@Entry
@Component
struct Index  {
  build() {
    Column() {
      Column() {
        Image($r("app.media.startIcon"))
          .width(100)
          .aspectRatio(1)
          // 分别绑定该属性,且id值一样,通过路由跳转实现专场动画效果
          .sharedTransition("sharedID", {duration: 350})
        Button("跳转")
          .onClick(() => {
            router.pushUrl({
              url: "pages/Index2"
            })
          })
      }
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}
  • Index2
ts 复制代码
import { router } from '@kit.ArkUI';

@Entry
@Component
struct Index2 {
  @State message: string = 'Hello World';

  build() {
    Column() {
      Image($r("app.media.startIcon"))
        .width("100%")
        .height(400)
          // 分别绑定该属性,且id值一样,通过路由跳转实现专场动画效果
        .sharedTransition("sharedID", {duration: 350})
      Button("返回")
        .onClick(() => {
          router.pushUrl({
            url: "pages/Index"
          })
        })
    }
    .height('100%')
    .width('100%')
    .justifyContent(FlexAlign.SpaceBetween)
  }
}
相关推荐
Magic-ZYJ10 小时前
HarmonyOS Release 构建安全:ArkGuard 混淆、签名配置与秘密管理
安全·华为·harmonyos·移动应用开发·心晴手记
见山是山-见水是水11 小时前
拆解HarmonyOS网络请求适配:原生鸿蒙页面的实现路径与调试方法
华为·harmonyos
m0_7496902311 小时前
【寻迹校园 HarmonyOS NEXT 实战 36】深色模式不是反色:用语义 Token 管理品牌色与状态色
harmonyos·arkts·深色模式·ui设计·主题设计
lqj_本人12 小时前
鸿蒙跨端实战_QT后台切换导致OpenGLES上下文丢失:黑屏自救指南
qt·华为·harmonyos
OH_TPC14 小时前
HarmonyOS APP开发---“数据眼“健康追踪App,需要用到这个库
华为·harmonyos·鸿蒙
lilian23314 小时前
Harmony os 技术实战|拼豆制图49:五组分类视觉规则如何收口成一张配置表
华为·harmonyos
2501_9197490315 小时前
华为鸿蒙免费决策APP—小羊挠头
华为·harmonyos·鸿蒙
m0_7496902315 小时前
【寻迹校园 HarmonyOS NEXT 实战 37】一套 ArkUI 适配四档宽度:sm、md、lg、xl 响应式 Shell 实战
华为·harmonyos·arkui·navigation·响应式布局·多设备适配
RisunJan17 小时前
HarmonyOS 架构精读:从系统分层到应用模型
华为·架构·harmonyos
见山是山-见水是水17 小时前
从需求到页面:鸿蒙原生 Ability 与 ArkTS 交互的 ArkTS 原生实现
华为·harmonyos