Cody Albert
Cody Albert

Reputation: 51

Reusing Bitmaps in Android

I'm working on an Android game (2d platformer) and am running into Out of Memory issues. I've done everything I can to reduce the number of bitmaps being stored in memory at a given time, but I'm still having problems. A couple quick questions.

1) Does the following code correctly use one Bitmap for every instance of an enemy?

Bitmap pusher2 = BitmapFactory.decodeResource(res, R.drawable.pusher_white);
Bitmap stinger2 = BitmapFactory.decodeResource(res,R.drawable.stinger_glow_v2_135x135)
Bitmap lightTrap2 = BitmapFactory.decodeResource(res, R.drawable.light_trap_v3_64x33); 

Enemy e21 = new Enemy(assets, pusher2, Enemy.SLIME, 32, 42, 12, 12, 17*32, 61*32-32, -50, 0);
Enemy e22 = new Enemy(assets, pusher2, Enemy.SLIME, 32, 42, 12, 12, 27*32, 57*32-32, -50, 0);
Enemy e23 = new Enemy(assets, pusher2, Enemy.SLIME, 32, 42, 12, 12, 47*32, 34*32-32, -50, 0);
Enemy e24 = new Enemy(assets, pusher2, Enemy.SLIME, 32, 42, 12, 12, 10*32, 29*32-32, -50, 0);
Enemy e25 = new Enemy(assets, pusher2, Enemy.SLIME, 32, 42, 12, 12, 19*32, 29*32-32, 50, 0);
Enemy e26 = new Enemy(assets, pusher2, Enemy.SLIME, 32, 42, 12, 12, 35*32, 20*32-32, -50, 0);
Enemy e27 = new Enemy(assets, stinger2, Enemy.STINGER, 135, 135, 22, 22, 32*32, 57*32-135, 144, 0, 23*32, 42*32);
Enemy e28 = new Enemy(assets, stinger2, Enemy.STINGER, 135, 135, 22, 22, 30*32, 40*32-135, 144, 0, 26*32, 41*32);
Enemy e29 = new Enemy(assets, stinger2, Enemy.STINGER, 135, 135, 22, 22, 35*32, 6*32-135, 144, 0);
Enemy e2a = new Enemy(assets, lightTrap2, Enemy.LIGHT_TRAP, 33, 64, 5, 24, 30*32, 21*32-33, 0, 0);
Enemy e2b = new Enemy(assets, lightTrap2, Enemy.LIGHT_TRAP, 33, 64, 5, 24, 40*32, 21*32-33, 0, 0);
Enemy e2c = new Enemy(assets, lightTrap2, Enemy.LIGHT_TRAP, 33, 64, 5, 24, 34*32, 13*32-33, 0, 0);

2) My normal approach to changing directions is to replace the sprite sheet with a mirrored copy of the original. This approach works great for the player, but seems impossible for the enemies if they are all referring to the same sheet. Do I need to store both mirrored and unmirrored copies of every enemy sprite sheet in memory, or is there a better way?

3) Is anything stored on the native heap by my application other than Bitmaps? Is there any way to look at it and see what's on there? It's at about 6k after I recycle all of my bitmaps to load in a new level, I feel like that's a bit high..

Thanks in advance!

Upvotes: 1

Views: 1025

Answers (1)

Egor
Egor

Reputation: 40193

First, want to ask you a questions: when does the OutOfMemory happen? When you're loading the bitmaps, or in the middle of the game?

Here's the approach I'm using for long time now and it seems the most convenient to me:

  1. Create a class that will load and unload all the Bitmaps in your application.
  2. Let the references to Bitmap objects of your BitmapManager (let's call it that way) class be public.
  3. Don't hold references to the Bitmap objects inside your classes, just use the references to the BitmapManager objects inside the onDraw method of your objects.

When creating mirrored Bitmaps, be sure to not create them at runtime. Mirror them all when loading bitmaps, otherwise they'll kill your app. Hope this helps.

Upvotes: 1

Related Questions