DrawableCompat setTint displays all new Drawables with the same identifier

I have chats that I want to color in some situations:

Drawable bubbleDrawable = ContextCompat.getDrawable(context, R.drawable.bg_chat_bubble);

if (tint) {
    bubbleDrawable = DrawableCompat.wrap(bubbleDrawable);
    DrawableCompat.setTint(bubbleDrawable, bubbleTint);
}

The problem is that once when R.drawable.bg_chat_bubble(this is a 9-patch) was tinted, all calls ContextCompat.getDrawable(context, R.drawable.bg_chat_bubble)return a tinted image instead of orignal. Even when I close the chat and open a completely different chat, the bubbles there have the previous hue. Only killing the application helps restore the correct color. To the first shade ...

Even with direct adjustment , a tinted image instead of the original appears bubbleDrawable = ContextCompat.getDrawable(context, R.drawable.bg_chat_bubble)inside the hue branch after the call setTint.

I also tried getResources().getDrawable(R.drawable.bg_chat_bubble), but the result is the same. Therefore, as soon as I want to use a hue for any drawable resrouce, I should always set the hue for this resource, otherwise I get unpredictable results.

This happens on Android 5.1 (possibly others) and appcompat-v7:23.2.+and appcompat-v7:23.1.+. Is this a mistake, or am I doing something wrong?

+4
source share
1 answer

all you need to do is change your drawing method before setting the hue:

bubbleDrawable.mutate()

Drawable.mutate

drawable mutable. . , . , . , , ; , . Drawable .

+10

All Articles