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

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

相关推荐
梦想不只是梦与想6 分钟前
鸿蒙性能优化:启动速度
性能优化·harmonyos·启动速度
小雨青年2 小时前
【HarmonyOS 7开发者前瞻】10 HarmonyOS 7 真实项目适配路线图:API 26、AI / Agent 与多端改造优先级
人工智能·华为·harmonyos
aqi002 小时前
鸿蒙版本的小小机器人APP开放源码啦
人工智能·华为·harmonyos·鸿蒙·harmony
lmy_loveF2 小时前
华为模拟器添加卡片
华为·鸿蒙
qizayaoshuap3 小时前
鸿蒙 ArkTS 实战:搜索列表 城市实时过滤(示例 94)
android·华为·harmonyos
木木子224 小时前
# HarmonyOS ArkTS 井字棋小游戏深度解析 —— 从零构建完整的三子棋对战应用
华为·harmonyos
lmy_loveF4 小时前
HarmonyOS开发入门:DevEco Studio安装指南
华为·鸿蒙
FF2501_9402285816 小时前
HarmonyOS应用开发实战:猫猫大作战-在排行榜和历史记录查询中,排序是必不可少的操作【apple_product_name】
华为·harmonyos·鸿蒙
程序员黑豆21 小时前
鸿蒙应用开发:@Provider 与 @Consumer 跨组件双向同步详解
前端·harmonyos