• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Making an animation with BitmapDrawable

__Jon__

Newbie
I want to make a 5 frame animation. I got five PNGs, named frame1,frame2,... and put them into the res folder.

I then created Bitmaps, and converted the Bitmaps into BitmapDrawables.
And then added them to an AnimationDrawable inside an ImageView (ImV).

Here is the code.


Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),
R.drawable.frame5);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(),
R.drawable.frame4);
Bitmap bitmap3 = BitmapFactory.decodeResource(getResources(),
R.drawable.frame3);
Bitmap bitmap4 = BitmapFactory.decodeResource(getResources(),
R.drawable.frame2);
Bitmap bitmap5 = BitmapFactory.decodeResource(getResources(),
R.drawable.frame1);


BitmapDrawable bmp1 = new BitmapDrawable(bitmap1);
BitmapDrawable bmp2 = new BitmapDrawable(bitmap2);
BitmapDrawable bmp3 = new BitmapDrawable(bitmap3);
BitmapDrawable bmp4 = new BitmapDrawable(bitmap4);
BitmapDrawable bmp5 = new BitmapDrawable(bitmap5);

int duration = 100;
AnimationDrawable mAnim = new AnimationDrawable();
mAnim.addFrame(bmp1, duration);
mAnim.addFrame(bmp2, duration);
mAnim.addFrame(bmp3, duration);
mAnim.addFrame(bmp4, duration);
mAnim.addFrame(bmp5, duration);



ImV.setBackgroundDrawable(mAnim);
mAnim.setOneShot(false);
mAnim.start();

Unfortunately, all that happens is the first frame of animation bmp1 is shown. It doesn't cycle through the frames.
Could I get some advice how to do this properly?
 
Back
Top Bottom