[SOLVED] Android: how to use ValueAnimator


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

Issue

I want to do a translate animation using this following

public static void move(TextView view){

    ValueAnimator va = ValueAnimator.ofFloat(0f, 3f);
    int mDuration = 3000; //in millis
    va.setDuration(mDuration);
    va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        public void onAnimationUpdate(ValueAnimator animation) {

        }
    });
    va.setRepeatCount(5);
    va.start();
}

But I don’t know how to use onAnimationUpdate method.

Can anyone help please?

Solution

If you really, really, really want to use ValueAnimator for animating translation of the View you can do it this way (finishing your example, assuming you meant translationX.

Bare in mind that you’re animating translation from 0px to 3px, so you probably won’t see much difference.

fun move(view: TextView) {
  val va = ValueAnimator.ofFloat(0f, 3f)
  va.duration = 3000 //in millis
  va.addUpdateListener { animation -> view.translationX = animation.animatedValue as Float }
  va.repeatCount = 5
  va.start()
}

Answered By – Bartek Lipinski

people found this article helpful. What about you?