Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 771
Jin Avatar asked Feb 12 '23 14:02

Jin


1 Answers

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));
    }
});
like image 123
romtsn Avatar answered Feb 23 '23 20:02

romtsn