Background Bitmap image in Activity?

407 views Asked by At

I'm currently working on a simple Android game for school, and the game runs for a little bit before it runs out of memory and crashes. I suspect this has to do with the number of images I'm using in the game (small, 10-20 KB sized files for 10+ items/characters/buttons, and also a very large, 450 KB PNG file for a splash screen background).

I've been using BitmapFactory to decode all of my small item PNGs, and I also was able to set the background for my main GameView as so (only the bare minimum of example code is included):

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

private Bitmap background, invincibleBitmap;

//Sets the background bitmap to an image scaled to the same size as the user's screen
background = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.background), getWidth(), getHeight(), false);
invincibleBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.invincible_item), 60, 60, false);

For the splash screen I mentioned earlier, however, I've been using the Activity's Layout XML file to set the image as so:

  android:background="@drawable/main"

I want to be able to scale main.png the same way that I did in a View in an Activity, hoping that it would help the memory overload problem. How would I go about doing this? I'm a beginner with Android in general so the simpler the solution, the better.

1

There are 1 answers

1
hasan On BEST ANSWER

To point out, image file size don't effect memory usage. It depend on the image specs: width x height x 1 byte.

You shouldn't set splash screen image in code by scaling it depending on screen resolution. The drawable folders are there to do that. Provide a different image with different specs for each of the screen resolution categories (l,m,x,xx,xxx).

As a fast solution, copy and paste the same image to all the drawable folders. Surprisingly you will notice a difference.