Reputation: 4659
I've heard that using a list of Bitmaps for animation is a bad idea. But I had not run into a situation to prove that was true until now.
The code I have works great. But only on the emulator, or on my phone running Android 6. Anything lower than that and I get an Out of Memory before it finishes initializing.
This is how I'm loading the images in:
public static Image[] flameIs = new Image[300];
for (int i=0;i<300;i++) {
if (i>=10) framePref="000";
if (i>=100) framePref="00";
Assets.flameIs[i] = g.newImage("frames/lighter_" + framePref +i+ ".png", ImageFormat.RGB565);
}
So it's like 300 PNGs, 8bit, about 12k each in size. We're talking about less than 4MB worth of images.
All the app does later is run these frames in a loop forever.
Is there a way to avoid that "out of memory"?
Upvotes: 1
Views: 265
Reputation: 380
Instead of creating a 300 frame animation, why not just convert them into a GIF and use something like Glide to render it? (or even a webview if you don't want to add a new library to your project)
It'll give you better control and portability across platforms.
With Glide, it will look something like this:
ImageView imageView = (ImageView) findViewById(R.id.imageView); GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView); Glide.with(this).load(R.raw.sample_gif).into(imageViewTarget);
Upvotes: 0
Reputation: 4978
300 Bitmaps of 459x620 loaded as RGB_565 means that you take 300 * 459*620 * 2 = 171 MB of memory.
Looking at https://stackoverflow.com/a/9940415/3413324 which sum up the heap size for popular devices, we can see that your Bitmaps might exceeds the limit even for recent devices.
What you can do is :
Reduce the size of your Bitmaps so that they each need less space in memory
Reduce the number of Bitmaps you use for your animation, thus reducing the memory footprint
Use a GIF that you can load with a library. You can have then direct control of the size of a unique GIF file
Upvotes: 2