【arkts】HarmonyOS鸿蒙属性动画animation踩坑记录

【arkts】HarmonyOS鸿蒙属性动画animation踩坑记录

目标:点击方向按钮,图片小鱼设置animation属性实现平滑移动

Image($r('app.media.fish'))
          .position({x: this.fishX, y: this.fishY})
          .rotate({angle: this.angle, centerX: '50%', centerY: '50%'})
          .width(200)
          .interpolation(ImageInterpolation.High)
          .animation({duration: 1000})  //animation不生效

          Column(){
            Button('↑').backgroundColor('#20101010')
            .onClick(() => {
              this.fishY -= 20
            })
            Row({space: 40}){
              Button('←').backgroundColor('#20101010')
                .onClick(() => {
                this.fishX -= 20
              })
              Button('→').backgroundColor('#20101010')
                .onClick(() => {
                  this.fishX += 20
                })
            }
            Button('↓').backgroundColor('#20101010').onClick(() => {
              this.fishY += 20
            })
          }.position({x: 20, y: 230})

运行效果:
在这里插入图片描述

可以看出animation并没有起作用

Image($r('app.media.fish'))
          .position({x: this.fishX, y: this.fishY})
          .rotate({angle: this.angle, centerX: '50%', centerY: '50%'})
          .width(200)
          .height(100)  //设置height
          .interpolation(ImageInterpolation.High)
          .animation({duration: 1000})

          Column(){
            Button('↑').backgroundColor('#20101010')
            .onClick(() => {
              this.fishY -= 20
            })
            Row({space: 40}){
              Button('←').backgroundColor('#20101010')
                .onClick(() => {
                this.fishX -= 20
              })
              Button('→').backgroundColor('#20101010')
                .onClick(() => {
                  this.fishX += 20
                })
            }
            Button('↓').backgroundColor('#20101010').onClick(() => {
              this.fishY += 20
            })
          }.position({x: 20, y: 230})

运行效果:在这里插入图片描述

成功解决
其他animation不生效的原因可能有animation设置在了需要有动画属性之前,也不会生效,animation最好还是写在最后

Image($r('app.media.fish'))
            .position({x: this.fishX, y: this.fishY})
            //.animation({duration: 1000})  写在这里不会生效
            .rotate({angle: this.angle, centerX: '50%', centerY: '50%'})
            .width(200)
            .height(100)
            .interpolation(ImageInterpolation.High)
            .animation({duration: 1000})  // 正确

总结:起初因为只设置width即可实现对图片等比进行缩放,所以未设置height,导致出现了animation不生效,其中原因大概能猜到七七八八,但不知道如何表达也不是很确定,欢迎大佬为我解惑

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值