简体   繁体   English

如何使用ValueAnimator#ofFloat

[英]How to use ValueAnimator#ofFloat

I'm so confused as to how ValueAnimator works in Android. 我对ValueAnimator在Android中的工作方式感到困惑。 Here's a code snippet (pseudo): 这是一个代码片段(伪):

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

I see the following in my logs: 我在日志中看到以下内容:

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

Can someone explain to me why my update function is getting these strange values? 有人可以向我解释为什么我的更新功能得到这些奇怪的值吗?

Why don't you use getAnimatedValue ? 为什么不使用getAnimatedValue It's exactly that value that calculated while property changing, it's not a fraction: 正是在属性更改时计算出的那个值,而不是分数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM