ValueAnimator
suggest changeValueAnimator
introduces a simple way to animate a value (of a particular type, e.g. int
, float
, etc.).
The usual way of using it is:
- Create a
ValueAnimator
that will animate a value frommin
tomax
- Add an
UpdateListener
in which you will use the calculated animated value (which you can obtain withgetAnimatedValue()
)
There are two ways you can create the ValueAnimator
:
(the example code animates a float
from 20f
to 40f
in 250ms
)
- From
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();
- From the code:
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();
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents