鸿蒙:使用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 分钟前
HarmonyOS Next 项目完整学习指南
华为·harmonyos
爱笑的眼睛118 小时前
我的HarmonyOS百宝箱
华为·harmonyos
2501_919749038 小时前
鸿蒙:创建公共事件、订阅公共事件和退订公共事件
华为·harmonyos
我是华为OD~HR~栗栗呀9 小时前
华为od-21届考研-C++面经
java·c语言·c++·python·华为od·华为·面试
安防视频中间件/视频资源汇聚平台10 小时前
华为结构化摄像机接入到SVMSPro平台操作流程
华为·信息可视化·实时音视频·svmspro
颜颜yan_12 小时前
HarmonyOS 6 ArkUI框架实战:文本展开折叠组件开发详解
华为·harmonyos·arkui
Industio_触觉智能14 小时前
开源鸿蒙6.1和8.1版本被确定为LTS建议版本,最新路标正式发布!-转自开源鸿蒙OpenHarmony社区
harmonyos·openharmony·开源鸿蒙
●VON15 小时前
重生之我在大学自学鸿蒙开发第五天-《实战篇》
学习·华为·云原生·harmonyos·鸿蒙
饮马长城窟15 小时前
华为Asend NPU 大模型W8A8量化调优
华为
文火冰糖的硅基工坊16 小时前
[嵌入式系统-114]:华为的操作系统:鸿蒙操作系统(HarmonyOS)和欧拉操作系统(openEuler)
科技·华为·架构·harmonyos