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));
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
Related
I'm using an LruCache to cache a lot of small BitmapDrawables used throughout my app. The problem is that the sizes differ for the different places I use the images.
I set the bounds when I retrieve the drawable from the cache before setting it to the ImageView to have the correct size.
When I set the bounds, the drawable resizes on the other places as well.
How can I get around this issue without using drawable.getConstantState().newDrawable()? Creating a new drawable from the cached drawable is very slow when scrolling a listview.
The same images are used in DynamicDrawableSpans where I can't set the bounds on the span itself, only on the drawable directly.
Will it be wise to have the same drawable image cached for the different contexts in seperate Caches?
I got it to work correctly.
I changed the LruCache type from BitmapDrawable to Bitmap. This way the cache only stores the Bitmap image and not the drawable bounds as well.
When I want to set the bitmap I convert it to BitmapDrawable
Drawable d = new BitmapDrawable(getResources(), bitmap);
Then I set the bounds for the newly created drawable instance
d.setBounds(0, 0, sizex, sizey);
This way each instance of the Bitmap is a seperate Drawable with its own Bounds.
Is there a way to directly scale a bitmap (bigBmp) into another bitmap (smallBmp) whithout creating a new one?
This creates a new one:
Bitmap smallBmp = Bitmap.createScaledBitmap(bigBmp, 20, 20, false);
I need to scale it always to the same size so I'd rather create smallBmp in advance (with size 20x20) and reuse this one every time.
I know, creating a 20x20 bitmap is probably not worth the optimization but it's inside a heavy loop where I just don't want any allocations.
It is taking time when i tried to change the bitmap.
Setting a new bitmap into an ImageView can also be cause of slowdown since you're updating the image frequently.
Bitmap thumb = ThumbnailUtils.createVideoThumbnail("videofilepath", MediaStore.Images.Thumbnails.MINI_KIND);
image.setImageBitmap(thumb);
image.setImageResource(R.drawable.add);
I have a board game app that creates a lot (hundreds) of bitmaps, and constantly changes them as the game progresses. These bitmaps are created, not loaded from a resource... so they have no R.id to refer to.
I would like to animate some of them, e.g. a bitmap moving from one loction to another when a player taps to move it. What is the best way to do this?
Note, this is 2.1 and the bitmaps are drawn on a canvas via a matrix translate.
Make this function to get image from drawable folder
private Bitmap getImageBitmap(){
int resID = getResources().getIdentifier(uri, "drawable",
"a.b.c");
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), resID);
return mBitmap;
}
For set the image , where do you you want>
ImageView imageview01.setImageBitmap(getImageBitmap("name of bitmap");
if you want more about bitmap. then you may try this links.
http://developer.android.com/reference/android/widget/ImageView.html
drawable getResources().getIdentifier problem
setImageURI / setimagebitmap to fetch image from net and display in image view
I'm new to Android dev and I'm having a hard time trying to do something which seems obvious to me: drawing little images on top of a bigger image.
Let's say that I have a 500x500 image and I want to draw icons at different locations. Icons are png files that I load with:
Bitmap img =
BitmapFactory.decodeResource(getResources(),
R.drawable.idIcon1)
My "background image" is a LayerDrawable.
Then, I am totally lost... Do I have to create a canvas ? How to draw on my "background image" my icons at different positions?
int positionLeft=0;
int positionTop=0;
Bitmap newBitmap =Bitmap.createBitmap(backgroundBitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(backgroundBitmap, positionLeft, positionTop,null);
positionLeft=100;
positionTop=100;
canvas.drawBitmap(iconBitmap,positionLeft,positionTop,null);
imageView.setImageBitmap(newBitmap);
You're making simple things difficult. Just use a layout with android:background attribute, and then add ImageViews dynamically with the necessary bitmaps inside.