我对ValueAnimator在Android中的工作方式感到困惑。这是一个代码片段(伪):

Log("animating: ", start, end);
ValueAnimator animator = ValueAnimator.ofFloat(start, end);
animator.setUpdateListener(() -> {
    Log("update", animation.getAnimatedFraction());
});
animator.start();

我在日志中看到以下内容:
animating: 0.0 to 1.0
update: 0.0
update: 0.05
..
update: 1.0

animating: 1.0 to 0.0
update: 0.0 (wtf)
update: 0.05
..
update: 1.0

animating: 0.5 to 1.0
update: 1.0 (wtf)
update: 1.0

有人可以向我解释为什么我的更新功能得到这些奇怪的值吗?

最佳答案

为什么不使用getAnimatedValue?正是在属性更改时计算出的那个值,而不是分数:

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    public void onAnimationUpdate(ValueAnimator animation) {
        Float value = (Float) animation.getAnimatedValue();
        Log("update", String.valueOf(value));
    }
});

10-07 18:58
查看更多