CodeMonkey
CodeMonkey

Reputation: 1835

Efficient Image Resizing

In short, is the following an inefficient way to rescale images in any way perhaps due to garbage collection?

playBg_ = BitmapFactory.decodeResource(res_, R.drawable.field);
playBg_ = Bitmap.createScaledBitmap(playBg_, screenWidth_, screenHeight_, false);

And would it then be better to do something like this?

Bitmap tempBmp = BitmapFactory.decodeResource(res_, R.drawable.field);
playBg_ = Bitmap.createScaledBitmap(tempBmp, screenWidth_, screenHeight_, false);
tempBmp.recycle();

Or is there a better way?

Thanks!

Upvotes: 0

Views: 163

Answers (3)

CodeMonkey
CodeMonkey

Reputation: 1835

I found that the second method was required. The first method left too much memory wasted before garbage collection could get to it. I also found that I definitely need to call recycle on all my images prior to loading up new ones. Simply dereferencing them and expecting garbage collection to pick it up is not sufficient. I was constantly running out of memory due to this fact.

Upvotes: 0

yarons
yarons

Reputation: 294

Second way is better. But why are you decoding the Bitmap by yourself and changing its size?

If you want to display it as a background, just use

myBgView.setBackground(R.drawable.field);

on your activity parent view.

Upvotes: 0

minipif
minipif

Reputation: 4866

You can read your logcat output by installing aLogcat on your eeePad.

Upvotes: 1

Related Questions