Chapter 32 Property Animation
For the More Curious: Other Animation APIs
While property animation is the most broadly useful tool in the animation toolbox, it is not the only
one. Whether or not you are using them, it is a good idea to know about the other tools out there.
Legacy animation tools
One set of tools is the classes living in the android.view.animation package. This should not be
confused with the newer android.animation package, which was introduced in Honeycomb.
This is the legacy animation framework, which you should mainly know about so that you can ignore
it. If you see the word “animaTION” in the class name instead of “animaTOR”, that is a good sign that
it is a legacy tool you should ignore.
Transitions
Android 4.4 introduced a new transitions framework, which enables fancy transitions between view
hierarchies. For example, you might define a transition that explodes a small view in one activity into a
zoomed-in version of that view in another activity.
The basic idea of the transitions framework is that you can define scenes, which represent the state of a
view hierarchy at some point, and transitions between those scenes. Scenes can be described in layout
XML files, and transitions can be described in animation XML files.
When an activity is already running, as in this chapter, the transitions framework is not that useful. This
is where the property animation framework shines. However, the property animation framework is not
good at animating a layout as it is coming onto the screen.
Take CriminalIntent’s crime pictures as an example. If you were to try to implement a “zoom”
animation to the zoomed-in dialog of an image, you would have to figure out where the original image
was and where the new image would be on the dialog. ObjectAnimator cannot achieve an effect like
that without a lot of work. In that case, you would want to use the transitions framework instead.
Challenges
For the first challenge, add the ability to reverse the sunset after it is completed, so your user can press
for a sunset, and then press a second time to get a sunrise. You will need to build another AnimatorSet
to do this – AnimatorSets cannot be run in reverse.
For a second challenge, add a continuing animation to the sun. Make it pulsate with heat, or give it a
spinning halo of rays. (You can use the setRepeatCount(int) method on ObjectAnimator to make
your animation repeat itself.)
Another good challenge would be to have a reflection of the sun in the water.
Your final challenge is to add the ability to press to reverse the sunset scene while it is still happening.
So if your user presses the scene while the sun is halfway down, it will go right back up again
seamlessly. Likewise, if your user presses the scene while transitioning to night, it will smoothly
transition right back to a sunrise.