A (175)

(Tuis.) #1
CHAPTER 10: Android Animation: Making Your UI Designs Move 359

The Tag: Your Frame Container


Most of the 2D frame-based animation assets will be created by using an XML
parent tag and its playback and visibility configuration parameters. The primary configuration
parameter that you will be using is the android:oneshot parameter. This is the parameter that
controls whether the animation playback will be configured to loop continuously or play forever
(using the oneshot=“false” setting), or if it will be configured to play just one single time (using the
oneshot=“true” setting). One-single-time playback is usually used with an event handling setup,
because you want the animation to play one time whenever it is clicked. The continuous playback
method, on the other hand, is usually setup with a .start( ) method call inside of an on Create( )
method, so the animated design element continues playing (all the time, forever), somewhere on that
Activity’s display screen.


Later on, you will reference the XML file that contains this parent tag, and its
children tags, by using its first name (that is, the first part of the filename) without the extension,
just like you did with the multi-state ImageButton definition in the previous chapter. We will create a
frame animation XML definition file that uses an anim_milkyway.xml file name, but which references
this file in Android XML markup and Java code as: anim_milkyway. Once this is
defined using XML, you will be able to reference the frame-based animation that you have defined in
it from any of your UI or UX designs across your entire application.


Finally, there is an android:visibility parameter which you can utilize if you are going to control the
visibility of your AnimationDrawable object within your Java code. You can use this parameter to set
the initial visibility setting, which is usually going to be “true” or visible, until a user clicks it, or some
other code function hides it.


As you’ll see later, there is also a way to auto-start your animation via XML, so that you don’t have
to use an ID parameter, which is generally used to provide a way for your Java code to inflate and
reference your XML tag constructs.


The Tag: How to Add in Animation Frames


The tag will always be the parent tag, because it is designed to contain
tags, which will always be the child tags. The item tag is used to define the frames in your


tag, with one tag for each animation frame. There are two parameters
used inside of the tag: the android:drawable file name reference parameter, and the
android:duration parameter which specifies a frame display duration integer value in milliseconds.
A millisecond is one-thousandth of a second, so one second of frame duration would use a 1000 integer
value. All these tags exist inside of your parent container, in the order in
which they are to be displayed, just like you are loading the animation frames into a data array in
system memory, which, essentially, you are.

The math for calculating this duration value, which is ultimately going to represent the animation’s
frame rate, which is usually specified in frames per second, or FPS, is the number of seconds
you want the animation to last times 1000, divided by the number of frames that you have in your
animation. So if you want a Lunar Lander to rotate once every second, and you have 8 frames to
create the smooth illusion of motion (45 degrees of rotation per frame), then 1 times 1000 divided
by 8 gives you 125 , so you would use an android:duration="125" parameter for each tag.

Free download pdf