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.


Legacy Animations

Before ViewPropertyAnimator and the rest of the animator framework were added in API Level 11, we had the original Animation base class and specialized animations based upon it, like TranslateAnimation 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 Animation approach. 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.

Prerequisites

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.

It’s Not Just For Toons Anymore

Android has a package of classes (android.view.animation) dedicated 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 (RotateAnimation), and 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:

  1. You may be using a container that supports animating its contents, such as a ViewFlipper or TextSwitcher. These are typically subclasses of ViewAnimator and 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 View and what animation is used to animate “in” the replacement View.
  2. You can simply tell any View to startAnimation(), given the Animation to apply to itself. This is the technique we will be seeing used in the examples in this chapter.

A Quirky Translation

The preview of this section was stepped on by Godzilla.

Fading To Black. Or Some Other Color.

The preview of this section was lost in the sofa cushions.

When It’s All Said And Done

The preview of this section apparently resembled a Pokémon.

Loose Fill

The preview of this section was eaten by a grue.

Hit The Accelerator

The preview of this section will not appear here for a while, due to a time machine mishap.

Animate. Set. Match.

The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.

Active Animations

The preview of this section was fed to a gremlin, after midnight.