Jin
Jin

Reputation: 6145

How to use ValueAnimator#ofFloat

I'm so confused as to how ValueAnimator works in 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?

Upvotes: 4

Views: 3997

Answers (1)

romtsn
romtsn

Reputation: 12002

Why don't you use 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));
    }
});

Upvotes: 8

Related Questions