Android Tutorial

(avery) #1

By : Ketan Bhimani


280 

ImageView img = (ImageView)findViewById(R.id.ImageView1);
BitmapDrawable frame1 = (BitmapDrawable)getResources().
getDrawable(R.drawable.f1);
BitmapDrawable frame2 = (BitmapDrawable)getResources().
getDrawable(R.drawable.f2);
BitmapDrawable frame3 = (BitmapDrawable)getResources().
getDrawable(R.drawable.f3);
int reasonableDuration = 250;
AnimationDrawable mAnimation = new AnimationDrawable();
mAnimation.addFrame(frame1, reasonableDuration);
mAnimation.addFrame(frame2, reasonableDuration);
mAnimation.addFrame(frame3, reasonableDuration);
img.setBackgroundDrawable(mAnimation);


To name the animation loop continuously,we can call the
setOneShot() method:

mAnimation.setOneShot(false);


To begin the animation,we call the start() method:

mAnimation.start();


We can end our animation at any time using the stop() method:

mAnimation.stop();


Although we used an ImageView background in this example, you
can use a variety of different View widgets for animations. For
example, you can instead use the Image Switcher view and change
the displayed Drawable resource using a timer. This sort of
operation is best done on a separate thread. The resulting
animation might look something like Figure—you just have to
imagine it moving.

Working with Tweened Animations

With tweened animation, you can provide a single Drawable
resource—it is a Bitmap graphic, a ShapeDrawable, a TextView, or
any other type of View object—and the intermediate frames of the
Free download pdf