鸿蒙:使用animation或animateTo实现图片无限旋转效果

前言:

两者的区别:animation是被动的,animateTo是主动的。

我们还是老样子,跟着官方文档学习和实践,链接如下:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-explicit-animationhttps://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-explicit-animation

https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-animatorpropertyhttps://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-animatorproperty

以下是豆包基于官方文档的总结:

不多说了,直接上练习代码和运行效果图:

【说明:点击图片,触发animation属性;点击按钮"无限旋转",触发animateTo方法】

代码如下:

Index.ets

复制代码
@Entry
@Component
export struct Index {
  @State angle: number = 0;

  build() {
    Column() {
      Image($r('app.media.startIcon'))
        .width(200)
        .rotate({ angle: this.angle, centerX: '50%', centerY: '50%' })
        .onClick(() => {
          this.angle = 90
        })
        .animation({
          duration: 500, // 时长
          curve: Curve.Linear, // 速度
          delay: 100, // 延迟
          iterations: -1, // 循环
          playMode: PlayMode.Normal // 动画模式
        })

      Button("无限旋转")
        .onClick(() => {
          this.getUIContext()?.animateTo({
            duration: 100, // 时长
            curve: Curve.Linear, // 速度
            delay: 100, // 延迟
            iterations: -1,
            playMode: PlayMode.Normal // 动画模式
          },
            () => {
              this.angle = 360; // 旋转角度
            })
        })
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height('100%')
  }
}

觉得有用,可以点赞、收藏或关注

相关推荐
电化学仪器白超1 天前
④使用 PPTSYNC.exe 与华为手机拍照制作 GIF 动画
服务器·华为·智能手机
CN-cheng1 天前
Flutter项目在HarmonyOS(鸿蒙)运行报错问题总结
flutter·华为·harmonyos·flutter运行到鸿蒙
平平不平凡1 天前
鸿蒙音频播放器深度解析:从核心设计到完整实现
harmonyos
HMSCore1 天前
【FAQ】HarmonyOS SDK 闭源开放能力 — Form Kit
harmonyos
小Mei数码说1 天前
华为WATCH D2 & WATCH GT6:秋季来临,尊爱老人的智能守护
华为
极客范儿1 天前
华为HCIP网络工程师认证—网络参考模型
网络·华为
摘星编程1 天前
昇腾NPU性能调优实战:INT8+批处理优化Mistral-7B全记录
人工智能·华为·gitcode·昇腾
爱笑的眼睛112 天前
HarmonyOS数据存储Kit深度实践:从架构设计到性能优化
华为·harmonyos
爱笑的眼睛112 天前
HarmonyOS后台代理提醒Agent:构建智能提醒功能的深度解析
华为·harmonyos
爱笑的眼睛112 天前
ArkTS可选链与空值合并:提升HarmonyOS应用开发的安全性与简洁性
华为·harmonyos