Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 35  Material Design


This is great if you need to use a property animation. If you want to perform a framed animation, you
need to use another tool: the animated state list drawable.


The name “animated state list drawable” is a little confusing. It sounds similar to “state list animator,”
but the purpose is totally different. Animated state list drawables allow you to define images for each
state, like a normal state list drawable, but they also allow you to define frame animation transitions
between those states.


Back in Chapter 23, you defined a state list drawable for BeatBox’s sound buttons. If a sadistic
designer (like our own Kar Loong Wong) wanted to have a multiframe animation each time the button
was pressed, you could modify your XML to look like Listing 35.3. (This version would need to live
inside a res/drawable-21 directory because this feature is not supported prior to Lollipop.)


Listing 35.3  An animated state list drawable



android:drawable="@drawable/button_beat_box_pressed"
android:state_pressed="true"/>
android:drawable="@drawable/button_beat_box_normal" />

<transition
android:fromId="@id/released"
android:toId="@id/pressed">






...



Here, each item in the selector gets an ID. You can then define a transition between different IDs to
play a multiframe animation. If you want to provide an animation when you release the button, too,
that requires an additional transition tag.

Free download pdf