Chapter 2. Creating cool animations

 

In this chapter, you’ll learn about animations. You’ll find different examples that use a variety of APIs to add animations to your application widgets.

Hack 5 Snappy transitions with TextSwitcher and ImageSwitcher: Android v1.6+

Imagine you need to cycle through information in a TextView or in an ImageView. Some examples of this would be

  • Navigating through a list of dates with Left and Right buttons
  • Changing numbers in a date picker
  • Countdown clock
  • News headlines

Changing the contents of a view is a basic function of most applications, but it doesn’t have to be boring. If we use the default TextView, you’ll notice there’s no eye candy when we swap its content. It’d be nice to have a way to apply different animations to content being swapped. So to make our transitions more visually appealing, Android provides two classes called TextSwitcher and ImageSwitcher. TextSwitcher replaces a TextView and ImageSwitcher replaces an ImageView.

TextView and TextSwitcher work in a similar way. Suppose we’re navigating through a list of dates, as mentioned earlier. Every time the user clicks a button, we need to change a TextView’s content with each date. If we use a TextView, we’re swapping out some text in a view using mTextView.setText("something"). Our code should look something like the following:

Hack 6 Adding eye candy to your ViewGroup’s children: Android v1.6+

Hack 7 Doing animations over the Canvas: Android v1.6+

Hack 8 Slideshow using the Ken Burns effect: Android v1.6+