1

I have a Activity on in my game to browser levels. In this activity Have a HorizontalScrollViewer display 25 levels at a time. The levels are represented by LevelBrowserButtons which are custom components. Each LevelBrowserButton an image of the level and these images are not very big and never over 35K each.

So with 25 buttons it should not use more than a 1MB for images but even with a VM heap size of 24 I get "java.lang.OutOfMemoryError: bitmap size exceeds VM budget".

My code is below:

public class LevelBrowserButton extends LinearLayout 
{   
  int LayoutNumber = 1;


  public LevelBrowserButton(Context context, String layoutName, int layoutNumber) 
  {
    super(context);
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);                                      
    inflater.inflate(R.layout.levelbrowser_button, this);

    LayoutNumber = layoutNumber;

    // Set Layout Name
    TextView nameTextView = (TextView) findViewById(R.id.NameTextView);
    nameTextView.setText(layoutNumber + ": " + layoutName);

    // Set Layout Image
    int imageResource = this.getResources().getIdentifier("levelbrowser_" + layoutNumber, "drawable", "mobile.firegame");
    ImageView backgroundImageView = (ImageView)findViewById(R.id.BoardImageView);
    backgroundImageView.setImageResource(imageResource);

    // Set Status Image
    ImageView statusImageView = (ImageView)findViewById(R.id.StatusImageView);
    if (Settings.getLevelStats(layoutName).Completed)
        statusImageView.setImageResource(R.drawable.levelbrowser_completed);
    else
        statusImageView.setImageResource(R.drawable.levelbrowser_uncompleted);
  }
}

I have read quite a bit of threads of people having the same type of issue but could not figure it out. This is not to do with disposing of Bitmaps properly as I haven't even been able to get that far.

Jamie Hutton
  • 260
  • 3
  • 13
Moresby
  • 79
  • 7
  • 1
    What's the resolution (px*px) of your images? The fact that some compressed file format is 35KB says very little about the actual memory requirements of a Bitmap. – Philipp Reichart Oct 31 '11 at 12:00
  • So you've got Bitmaps worth of 350*300*3*8*25 (w*h*rbg*byte*images) bytes, i.e. 60 MB --- depending on screen size you probably can't display more than 2-3 of them at any one time, you might want to consider http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview – Philipp Reichart Oct 31 '11 at 12:31
  • Thanks Philipp. I'll have a look and try to implement that and report back. – Moresby Oct 31 '11 at 12:51
  • Philipp, the link you posted is all to do with ListViews and since my game is all in landscape mode and I want to scroll horizontal it wont work for me. But I do realize I will not be able to have all the buttons added at one time. I will implement some kind of pager of sorts. Thank you for your help. – Moresby Nov 01 '11 at 08:03

1 Answers1

0

Here is a similar question that I answered and show how to go about dynamically loading the proper image size.

out of memory exception + analyzing hprof file dump

toidiu
  • 965
  • 2
  • 12
  • 25