HarmonyOS 鸿蒙Next:求助各位大佬,我想打印animateTo动画过程中的一系列动画值,要怎么做呢?

HarmonyOS 鸿蒙Next:求助各位大佬,我想打印animateTo动画过程中的一系列动画值,要怎么做呢?

@Entry
@Component
struct LayoutChange2 {
  @State myWidth: number = 100;
  @State myHeight: number = 50;
  // 标志位,true和false分别对应一组myWidth、myHeight值
  @State flag: boolean = false;

  build() {
    Column({ space: 10 }) {
      Button("text")
        .type(ButtonType.Normal)
        .width(this.myWidth)
        .height(this.myHeight)
        .margin(20)
      Button("area: click me")
        .fontSize(12)
        .margin(20)
        .onClick(() => {
          animateTo({ duration: 1000, curve: Curve.Ease }, () => {
            // 动画闭包中根据标志位改变控制第一个Button宽高的状态变量,使第一个Button做宽高动画
            if (this.flag) {
              this.myWidth = 100;
              this.myHeight = 50;
            } else {
              this.myWidth = 200;
              this.myHeight = 100;
            }
            this.flag = !this.flag;
          });
        })
    }
    .width("100%")
    .height("100%")
  }
}

比如像动画文档的例子,我想把myWidth变化时的值打印出来,并且用来干除了更新UI组件的其他事情。但animateTo除了能设置一个onFinish的监听,无法设置动画更新的监听。这种情况我该怎么做?

或者有其他能直接产生一系列动画值的方法吗?我现在急需一个动画值的发生器来做除了更新UI组件的其他事情。

谢谢🙏

3 回复

更多关于HarmonyOS 鸿蒙Next:求助各位大佬,我想打印animateTo动画过程中的一系列动画值,要怎么做呢?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


官方有个能算插值的库,你api里找一下,叫curve啥来着

在HarmonyOS鸿蒙Next系统中,若你想在animateTo动画过程中打印一系列动画值,可以通过监听动画的进度来实现。通常,动画类会提供诸如addAnimatorListeneraddUpdateListener等方法来让你在动画的不同阶段获取相关信息。

具体步骤如下:

  1. 设置动画监听器:使用addUpdateListener方法,该方法允许你在动画的每一帧更新时被调用。
  2. 获取动画值:在监听器的回调中,通过AnimatorUpdateListeneronAnimationUpdate方法,你可以获取当前的动画属性值。
  3. 打印值:在onAnimationUpdate方法中,使用日志打印函数(如Log.i)来输出当前的动画值。

示例代码(伪代码):

ValueAnimator animator = ValueAnimator.ofFloat(startValue, endValue);
animator.addUpdateListener(new AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float currentValue = (float) animation.getAnimatedValue();
        Log.i("AnimationValue", "Current value: " + currentValue);
    }
});
animator.start();

确保你的动画对象正确初始化,并且监听器已正确添加。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

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