Drawable setAlpha does not work on android 4.4.2

I use the following function to enable and disable drawings ...

public static void setDrawableState(Drawable d, boolean enabled)
{
    if (d == null)
        return;
    d.mutate(); // so drawables don't share state anymore
    if (enabled)
        d.setAlpha(255);
    else
        d.setAlpha(100);
}

This worked on all the phones that I have tried, now I see that it does not work on Android 4.4.2 (maybe not even on a specific version).

Is there any other (better) way to set the alpha method for drawing? Or am I missing something?

+4
source share
2 answers

Since drawables can use the same state, changing the state with the ability to draw will have no effect. You need to mutate the extraction, for example, in your code, try something like:

d.mutate().setAlpha(100);

Android , .

+3

:

Drawable d2 = d.getConstantState().newDrawable().mutate();
d2.setAlpha(100)
Hide result
0

All Articles