The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
ViewPropertyAnimator and the rest of the animator framework
were added in API Level 11, we had the original
class and specialized animations based upon it, like
for movement and
AlphaAnimation for fades. On the whole, you will want
to try to use the animator framework where possible, as the new system
is more powerful and efficient than the legacy
However, particularly for apps where the NineOldAndroids backport is
insufficient, you may wish to use the legacy framework.
After an overview of the role of the animation framework, we go in-depth to animate the movement of a widget across the screen. We then look at alpha animations, for fading widgets in and out. We then see how you can get control during the lifecycle of an animation, how to control the acceleration of animations, and how to group animations together for parallel execution. Finally, we see how the same framework can now be used to control the animation for the switching of activities.
Understanding this chapter requires that you have read the core chapters, particularly the ones on basic resources and basic widgets. Also, you should read the chapter on custom views.
Android has a package of classes (
to animating the movement and behavior of widgets.
They center around an
Animation base class that describes what is
to be done. Built-in animations exist to move a widget
TranslateAnimation), change the transparency of a widget
AlphaAnimation), revolve a widget (
resize a widget (
ScaleAnimation). There is even a way to
aggregate animations together into a composite
Animation called an
AnimationSet. Later sections in this chapter will examine the use
of several of these animations.
Given that you have an animation, to apply it, you have two main options:
TextSwitcher. These are typically subclasses of
ViewAnimatorand let you define the “in” and “out” animations to apply. For example, with a
ViewFlipper, you can specify how it flips between
Views in terms of what animation is used to animate “out” the currently-visible
Viewand what animation is used to animate “in” the replacement
startAnimation(), given the
Animationto apply to itself. This is the technique we will be seeing used in the examples in this chapter.
The preview of this section was stepped on by Godzilla.
The preview of this section was lost in the sofa cushions.
The preview of this section apparently resembled a Pokémon.
The preview of this section was eaten by a grue.
The preview of this section will not appear here for a while, due to a time machine mishap.
The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.
The preview of this section was fed to a gremlin, after midnight.