ValueAnimator
introduces a simple way to animate a value (of a particular type, e.g. int
, float
, etc.).
The usual way of using it is:
ValueAnimator
that will animate a value from min
to max
UpdateListener
in which you will use the calculated animated value (which you can obtain with getAnimatedValue()
)There are two ways you can create the ValueAnimator
:
(the example code animates a float
from 20f
to 40f
in 250ms
)
xml
(put it in the /res/animator/
):<animator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="250"
android:valueFrom="20"
android:valueTo="40"
android:valueType="floatType"/>
ValueAnimator animator = (ValueAnimator) AnimatorInflater.loadAnimator(context,
R.animator.example_animator);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator anim) {
// ... use the anim.getAnimatedValue()
}
});
// set all the other animation-related stuff you want (interpolator etc.)
animator.start();
ValueAnimator animator = ValueAnimator.ofFloat(20f, 40f);
animator.setDuration(250);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator anim) {
// use the anim.getAnimatedValue()
}
});
// set all the other animation-related stuff you want (interpolator etc.)
animator.start();