Reputation: 91
In the code below I've got two bitmaps (I omitted the code for creating them as that's not relevant to my question) and I also have an ImageView in my layout. I make the ImageView display the first bitmap as a drawable, and then make it display the second bitmap, again as a drawable.
I'm aware that bitmaps can be recycled, my question is related to the "new BitmapDrawable" part, as I can't figure out exactly what a BitmapDrawable is. Is it just a reference or does it use up memory each time one is created? In other words, does the BitmapDrawable I create for bitmap1 need to be deleted/recycled before I create another BitmapDrawable for bitmap2?
Thanks.
Bitmap bitmap1,bitmap2;
...assume bitmap1 and bitmap2 contain valid bitmaps...
// get imageview
ImageView iv = (ImageView)findViewById(R.id.my_imageview);
// make the imageview display bitmap1
iv.setImageDrawable(new BitmapDrawable(getResources(),bitmap1));
// now make the imageview display bitmap2
iv.setImageDrawable(new BitmapDrawable(getResources(),bitmap2));
Upvotes: 3
Views: 2460
Reputation: 1996
You should keep bitmaps as long as you need them. It is costly to create new bitmaps. GC will fire if enough memory not available and your app will stall for that time.
See this for efficiently displaying bitmaps http://developer.android.com/training/displaying-bitmaps/index.html
Upvotes: 1