LRUCache BitmapDrawable

272 views Asked by At

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?

1

There are 1 answers

0
Pierre On BEST ANSWER

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.