鸿蒙:使用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%')
  }
}

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

相关推荐
安卓开发者5 小时前
鸿蒙NEXT分布式文件系统:开启跨设备文件访问新时代
华为·harmonyos
程序员潘Sir7 小时前
鸿蒙应用开发从入门到实战(十四):ArkUI组件Column&Row&线性布局
harmonyos·鸿蒙
我是华为OD~HR~栗栗呀7 小时前
20届-高级开发(华为oD)-Java面经
java·c++·后端·python·华为od·华为
阿里云云原生13 小时前
移动端性能监控探索:鸿蒙 NEXT 探针架构与技术实现
华为·架构·harmonyos
SmartBrain14 小时前
华为昇腾 950 系列芯片深度解析
服务器·华为·gpu算力
我是华为OD~HR~栗栗呀15 小时前
华为od-前端面经-22届非科班
java·前端·c++·后端·python·华为od·华为
351868039916 小时前
鸿蒙音乐应用开发:打造跨设备的音乐体验
华为·harmonyos
351868039916 小时前
鸿蒙音乐应用开发:音频播放与UI交互实战
华为·音视频·交互·harmonyos·arkts