HarmonyOS 鸿蒙Next 动画如何按倍数缩放

HarmonyOS 鸿蒙Next 动画如何按倍数缩放

文档库里的范例代码,我想让@State wid: number = 100和@State hei: number = 100,按倍数缩放,设置20%,然后动画开始时变成宽高30%,再缩成20%,如何设置?下面的代码是按具体数值设置的,我想按百分比

import { Animator as animator, AnimatorResult } from '@kit.ArkUI';
@Entry
@Component
struct AnimatorTest {
private TAG: string = '[AnimatorTest]'
private backAnimator: AnimatorResult | undefined = undefined
private flag: boolean = false
[@State](/user/State) wid: number = 100
[@State](/user/State) hei: number = 100

create() {
this.backAnimator = animator.create({// 建议使用 this.getUIContext.creatAnimator()接口
duration: 2000,
easing: "linear",
delay: 0,
fill: "forwards",
direction: "alternate",
iterations: -1,
begin: 100,
end: 200
})
this.backAnimator.onFinish = ()=> {
this.flag = true
console.info(this.TAG, 'backAnimator onfinish')
}
this.backAnimator.onRepeat = ()=> {
console.info(this.TAG, 'backAnimator repeat')
}
this.backAnimator.onCancel = ()=> {
console.info(this.TAG, 'backAnimator cancel')
}
this.backAnimator.onFrame = (value:number)=> {
this.wid = value
this.hei = value
}
}

aboutToDisappear() {
// 由于backAnimator在onframe中引用了this, this中保存了backAnimator,
// 在自定义组件消失时应该将保存在组件中的backAnimator置空,避免内存泄漏
this.backAnimator = undefined;
}

build() {
Column() {
Column() {
Column()
.width(this.wid)
.height(this.hei)
.backgroundColor(Color.Red)
}
.width('100%')
.height(300)

Column() {
Row() {
Button('create')
.fontSize(30)
.fontColor(Color.Black)
.onClick(() => {
this.create()
})
}
.padding(10)

Row() {
Button('play')
.fontSize(30)
.fontColor(Color.Black)
.onClick(() => {
this.flag = false
if(this.backAnimator){
this.backAnimator.play()
}
})
}
.padding(10)

Row() {
Button('pause')
.fontSize(30)
.fontColor(Color.Black)
.onClick(() => {
if(this.backAnimator){
this.backAnimator.pause()
}
})
}
.padding(10)

Row() {
Button('finish')
.fontSize(30)
.fontColor(Color.Black)
.onClick(() => {
this.flag = true
if(this.backAnimator){
this.backAnimator.finish()
}
})
}
.padding(10)

Row() {
Button('reverse')
.fontSize(30)
.fontColor(Color.Black)
.onClick(() => {
this.flag = false
if(this.backAnimator){
this.backAnimator.reverse()
}
})
}
.padding(10)

Row() {
Button('cancel')
.fontSize(30)
.fontColor(Color.Black)
.onClick(() => {
if(this.backAnimator){
this.backAnimator.cancel()
}
})
}
.padding(10)

Row() {
Button('reset')
.fontSize(30)
.fontColor(Color.Black)
.onClick(() => {
if (this.flag) {
this.flag = false
if(this.backAnimator){
this.backAnimator.reset({
duration: 3000,
easing: "ease-in",
delay: 0,
fill: "forwards",
direction: "alternate",
iterations: 3,
begin: 100,
end: 300
})
}
} else {
console.info(this.TAG, 'Animation not ended')
}
})
}
.padding(10)
}
}
}
}
2 回复

关于多段动画,推荐使用关键帧动画实现:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-keyframeanimateto-V5
具体实现,您可以参考demo:

// xxx.ets
import { UIContext } from '@kit.ArkUI';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct KeyframeDemo {
  [@State](/user/State) myScale: number = 1.0;
  uiContext: UIContext | undefined = undefined;
  aboutToAppear() {
    this.uiContext = this.getUIContext?.();
  }
  build() {
    Column() {
      Circle()
        .width(100)
        .height(100)
        .fill("#46B1E3")
        .margin(100)
        .scale({ x: this.myScale, y: this.myScale })
        .onClick(() => {
          if (!this.uiContext) {
            console.info("no uiContext, keyframe failed");
            return;
          }
          this.myScale = 1;
          // 设置关键帧动画整体播放1次
          this.uiContext.keyframeAnimateTo({ iterations: 1 }, [
            {
              // 第一段关键帧动画时长为800ms,scale属性做从1到1.2的动画
              duration: 800,
              event: () => {
                this.myScale = 1.2;
              }
            },
            {
              // 第二段关键帧动画时长为800ms,scale属性做从1.2到1.3的动画
              duration: 800,
              event: () => {
                this.myScale = 1.3;
              }
            },
            {
              // 第三段关键帧动画时长为800ms,scale属性做从1.3到1.2的动画
              duration: 800,
              event: () => {
                this.myScale = 1.2;
              }
            }
          ]);
        })
    }.width('100%').margin({ top: 5 })
  }
} 

更多关于HarmonyOS 鸿蒙Next 动画如何按倍数缩放的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next系统中,动画按倍数缩放通常涉及对系统动画效果的设置调整,以下是一些具体的操作步骤:

  1. 开启开发者模式

    • 进入设置,下滑找到“关于手机”,点击进入“Harmony OS版本”,连续点击7次直到弹出“密码”输入界面,开启开发者模式。
  2. 调整动画缩放倍数

    • 返回设置页面,下滑找到“系统和更新”,点击进入“开发人员选项”。
    • 在开发人员选项中,分别找到“窗口动画缩放”和“过渡动画缩放”,点击进入并选择“动画缩放0.5x”以实现动画效果的加速,或者选择其他倍数以调整动画速度。

请注意,调整动画缩放倍数可能会影响系统的整体流畅性和视觉效果,因此建议根据个人喜好和需求进行设置。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!