I create a BitmapDrawable that I later assign to several Objects in a function like this (not exactly like this but its stripped down for testing purposes):
Drawable CreateBitmap(int col)
{ Bitmap b = Bitmap.createBitmap(50, 50, Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawColor(col);
return new BitmapDrawable(b);
}
And this works and creates me a BitmapDrawable with the size 50 x 50 that I keep in memory. I then assign this BitmapDrawable to various buttons, checkboxes, whatever comes up and needs a certain color.
the problem I have is that the BitmapDrawables are somehow getting truncated. It appears that if I assign it to a button with the height of 80 and then a button with the height of 160, the first is filled nicely and the latter is only filled by half. It appears the BitmapDrawable gets set to the first usage of it and all subsequent assignments are restricted by it. Sometimes it happens immediately sometimes it only happens when I switch to another tab and return back to the previous one.
I thought the BitmapDrawable would remain at 50×50 in memory and the object its assigned to is either getting its own version with an apropriate size (or makes a resized copy when rendering) and not that it somehow changes the BitmapDrawable itself. I admit, I am not sure how all of this works in depth and I would appreciate any advice on what I am doing wrong and if I do have to create individual BitmapDrawables for each object I assign them to or if I can do something else.
The BitmapDrawable is a Drawable object, containing methods such as setState that are pretty specific to an instance of the object. So, no, I don’t think a BitmapDrawable can be easily shared. (You might be able to reuse them successfully, much as ListView caches the Views for list members; but asking a BitmapDrawable to live in two or more places at the same time seems like it’s asking too much of it.)