Android value animator example

In this tutorial we will learn how to animate a value in android . We can implement this fumctionality help of ValueAnimator.This class is having a timing engine for animations which calculates animated values and set it in a view. Value animator is having an interface which will get updated in each frames.

For implementing a value animator we just need to implement the following function in your class file.

public void animateValue(float initialval, float finalval, final TextView textview) {

ValueAnimator valueAnimator = ValueAnimator.ofFloat(initialval,finalval);
valueAnimator.setDuration(25000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {

Double val=Double.parseDouble(valueAnimator.getAnimatedValue().toString());

textview.setText(val);

}
});
valueAnimator.start();
}

Now suppose you are having a TextView in your activity as follows

TextView tv_your_textview = (TextView)  findViewById(R.id.tv_your_textview );

For animating TextView value you just need to add the following code

animateValue(0,5,tv_your_textview);

In the above function there is a line in like follows

valueAnimator.setDuration(25000);

In which we are setting the duration of the animation.You can set your own values instead of 2500 if you want to change the length of duration. But if you didn’t set anything the default duration will be taken as 300 milliseconds.

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *