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();
}
});
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With