Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AnimationDrawable programmatically without animation-list xml

I am in a situation where I want to display animation from dynamic list of images. I want to use AnimationDrawable for this purpose, but unfortunately I am stuck... :( my code is as

ImageView globe = (ImageView) findViewById(R.id.globe);

AnimationDrawable animation = new AnimationDrawable();
animation.setOneShot(true);

Resources res = this.getResources();

while (from <= to) {
    String name = "globe_" + String.format("%03d", from);
    int globeId = res.getIdentifier(name, "drawable",
            this.getPackageName());
    animation.addFrame(res.getDrawable(globeId), 200);
    from++;
}

globe.setBackgroundDrawable(animation);
globe.post(new Runnable(){
        @Override
        public void run() {
            animation.start();
        }
});
like image 808
makki Avatar asked Apr 13 '12 23:04

makki


2 Answers

I just did animation.start() after the loop and called the function from a runnable and it worked

while (from <= to) {
    String name = "globe_" + String.format("%03d", from);
    int globeId = res.getIdentifier(name, "drawable",
            this.getPackageName());

    animation.addFrame(res.getDrawable(globeId), 200);
    from++;
}

globe.setBackgroundDrawable(animation);

animation.start()
like image 176
makki Avatar answered Sep 16 '22 15:09

makki


So Animation.addFrame(Drawable frame, int duration) takes a drawable which you can create from a Bitmap.

If your list of images are dynamically loaded:

List<Bitmap> images;
int duration = 200;    

for(Bitmap image: images){
    BitmapDrawable frame = new BitmapDrawable(image);
    animation.addFrame(frame, duration);
}

Try this out, should definitely work.

like image 31
Jian Chen Avatar answered Sep 17 '22 15:09

Jian Chen