In many cases, you want to play an animation that depends on when
another animation starts or finishes. The Android system lets you bundle
animations together into an
The following sample code taken from the Bouncing Balls sample (modified for simplicity) plays the following
You can listen for important events during an animation's duration with the listeners described below.
For example, the Bouncing Balls sample in the API demos creates an
AnimatorSet, so that you can
specify whether to start animations simultaneously, sequentially, or
after a specified delay. You can also nest AnimatorSet objects within each other.The following sample code taken from the Bouncing Balls sample (modified for simplicity) plays the following
Animator objects in the following manner:- Plays
bounceAnim. - Plays
squashAnim1,squashAnim2,stretchAnim1, andstretchAnim2at the same time. - Plays
bounceBackAnim. - Plays
fadeAnim.
AnimatorSet bouncer = new AnimatorSet(); bouncer.play(bounceAnim).before(squashAnim1); bouncer.play(squashAnim1).with(squashAnim2); bouncer.play(squashAnim1).with(stretchAnim1); bouncer.play(squashAnim1).with(stretchAnim2); bouncer.play(bounceBackAnim).after(stretchAnim2); ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f); fadeAnim.setDuration(250); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.play(bouncer).before(fadeAnim); animatorSet.start();For a more complete example on how to use animator sets, see the Bouncing Balls sample in APIDemos.
Animation Listeners
You can listen for important events during an animation's duration with the listeners described below.
Animator.AnimatorListeneronAnimationStart()- Called when the animation starts.onAnimationEnd()- Called when the animation ends.onAnimationRepeat()- Called when the animation repeats itself.onAnimationCancel()- Called when the animation is canceled. A cancelled animation also callsonAnimationEnd(), regardless of how they were ended.
ValueAnimator.AnimatorUpdateListeneronAnimationUpdate()- called on every frame of the animation. Listen to this event to use the calculated values generated byValueAnimatorduring an animation. To use the value, query theValueAnimatorobject passed into the event to get the current animated value with thegetAnimatedValue()method. Implementing this listener is required if you useValueAnimator.Depending on what property or object you are animating, you might need to callinvalidate()on a View to force that area of the screen to redraw itself with the new animated values. For example, animating the color property of a Drawable object only cause updates to the screen when that object redraws itself. All of the property setters on View, such assetAlpha()andsetTranslationX()invalidate the View properly, so you do not need to invalidate the View when calling these methods with new values.
AnimatorListenerAdapter class instead of implementing the Animator.AnimatorListener interface, if you do not want to implement all of the methods of the Animator.AnimatorListener interface. The AnimatorListenerAdapter class provides empty implementations of the methods that you can choose to override.For example, the Bouncing Balls sample in the API demos creates an
AnimatorListenerAdapter for just the onAnimationEnd() callback:ValueAnimatorAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f); fadeAnim.setDuration(250); fadeAnim.addListener(new AnimatorListenerAdapter() { public void onAnimationEnd(Animator animation) { balls.remove(((ObjectAnimator)animation).getTarget()); }
No comments:
Post a Comment