Wednesday, 7 August 2013

Drawable single instance grayscale. Mutate making many unnecessary copies?

Drawable single instance grayscale. Mutate making many unnecessary copies?

I want to make the background of a textview grayscale. Without mutate all
textviews that share the same background gets grayed at the same time even
though they should be independent.
My code right now looks like this:
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
getBackground().mutate().setColorFilter(filter);
This however, I believe, creates a new isntance of the drawable for each
textview. If I have, let's say 10 textview that share a common drawable
background, there will be 10 instances of the drawable even though it only
has 2 "states": colored and grayed.
Is it fine to use mutate like this? Or can I somehow dynamically create a
grayscaled version of the drawable if it hasn't been created yet?
PS. I have many different background images to choose from.

No comments:

Post a Comment