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

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

相关推荐
ZZZMMM.zip27 分钟前
演示架构师-PPT大纲生成的HarmonyOS开发实践
人工智能·华为·powerpoint·harmonyos·鸿蒙·鸿蒙系统
国服第二切图仔1 小时前
HarmonyOS APP《画伴梦工厂》开发第60篇-分布式软总线2.0——多设备协同新范式
分布式·wpf·harmonyos
2501_918582371 小时前
50 Canvas 动画:@State + @Watch + animateTo 驱动模式
华为·harmonyos·鸿蒙系统
xianjixiance_1 小时前
43 短距通信场景汇总:NFC / 二维码 / 剪贴板 / 超级终端
华为·harmonyos·鸿蒙系统
木木子222 小时前
[特殊字符] 音乐播放器——状态驱动的多媒体控制
android·开发语言·华为·php·harmonyos
拥抱太阳06162 小时前
HarmonyOS 应用开发《掌上英语》第2篇:ArkTS 模块化开发:Index.ets 统一导出模式深度解析
pytorch·ubuntu·harmonyos
不羁的木木2 小时前
《HarmonyOS技术精讲-Core Vision Kit》第4篇:人脸检测与关键点定位
华为·harmonyos
拥抱太阳06163 小时前
HarmonyOS 应用开发《掌上英语》第3篇:响应式架构:BreakpointModel 与多设备适配方案
华为·架构·harmonyos
w139548564224 小时前
双色叠加雷达图:DuetReport 双人对比
华为·harmonyos·鸿蒙系统
星释4 小时前
鸿蒙智能体开发实战:19.使用变量
华为·ai·harmonyos·鸿蒙·智能体