A (175)

(Tuis.) #1

372 CHAPTER 10: Android Animation: Making Your UI Designs Move


The Animation class is used to create the subclasses which are actually used via the XML tags,
which we will learn how to implement a bit later on in the chapter. There is one subclass for each
of the four types of tween animation you learned about in the previous section, including the
AlphaAnimation, RotateAnimation, ScaleAnimation, and TranslateAnimation classes. There is
also an AnimationSet class which is used to create groupings of more complicated tween animation
transforms called, you guessed it: Animation Sets.


Since we have already covered all of the XML parameters which can be used with the Android
Animation class, and therefore with any of its subclasses, in previous sections of this chapter, let’s
jump right into some hands-on XML markup and create your /res/anim folder and a procedural
animation asset to go inside of that folder that takes a spaceship image and applies a procedural
animation transform to it to animate it, so you can start to learn about how to create tween animation
in Android. After you create a tween animation spaceship, we will create a third spaceship, using
both frame and tween animation together, for the ultimate in Android animation!


Creating Tween Animation Using XML Markup


The simplest way to create one of these tween animation assets is to define your procedural animation
using an XML file,which will be stored in the HelloUniverse project’s /res/anim folder. We will create
this folder before we create an Animation object XML definition which will live inside of that folder.
The work process is similar to what you did previously to create a /res/drawable folder and frame
animation drawable XML asset definition.


Since we are creating three spaceship animations during this chapter, using both an
AnimationDrawable object and an Animation object, we will create our second spaceship animation
using an image of a different type of spaceship that one might find in the Andromeda galaxy and
apply a rotate transform to that image procedurally to make it spin slowly through the galaxy. After
that, I’ll show you how to combine frame and tween animation together, providing you the ultimate
2D animation weaponry possible for your Android programming arsenal.


After we create the Animation object using an XML definition, we will attach the animation data to a
spaceship digital image called friendship.png. I got this spaceship off of the NASA website in the
3D Studio .3DS format. I rendered the model in a simple black and blue color using 3D Studio MAX



  1. After you create a /res/anim folder and XML file containing procedural animation parameters,
    we will get back into Java coding and wire up ImageButton UI elements so I can show you how to
    setup and trigger frame animation and tween animation.


Create an /anim Folder: Tween Animation Assets


Right-click on your /res folder, select New ➤ Folder, and name the folder anim, as is shown in
Figure 10-9.

Free download pdf