Skip to content Skip to sidebar Skip to footer

Setcolorfilter() Broken On Android 4, Working On Android 5

I am trying to flash different colours onto the screen at regular intervals (a few times per second). To change the colours, I use Drawable.setColorFilter(int color, Mode mode) on

Solution 1:

Ultimately, it seems like the problem is that KitKat doesn't support using a ColorFilter (or implicitly an alpha) on a Drawable that will in turn be in a StateListDrawable. My solution was to use the same to code to construct the complex Drawable and then render that into a simple BitMapDrawable:

static Drawable createDrawable(Context context, int color, boolean disabled) {
OvalShapeoShape=newOvalShape();
ShapeDrawablebackground=newShapeDrawable(oShape);
background.getPaint().setColor(color);

ShapeDrawableshader=newShapeDrawable(oShape);
shader.setShaderFactory(newShapeDrawable.ShaderFactory() {
    @Overridepublic Shader resize(int width, int height) {
        returnnewLinearGradient(0, 0, 0, height,
                newint[]{
                        Color.WHITE,
                        Color.GRAY,
                        Color.DKGRAY,
                        Color.BLACK
                }, null, Shader.TileMode.REPEAT);
    }
});

Drawableicon= ContextCompat.getDrawable(context, R.drawable.ic_chat_button).mutate();
icon.setColorFilter(context.getResources().getColor(R.color.control_tint_color), PorterDuff.Mode.SRC_IN);

Drawablelayer=newLayerDrawable(newDrawable[]{ shader, background, icon });
layer.setAlpha(disabled ? 128 : 255);

// Note that on KitKat, setting a ColorFilter on a Drawable contained in a StateListDrawable//  apparently doesn't work, although it does on later versions, so we have to render the colored//  bitmap into a BitmapDrawable and then put that into the StateListDrawableBitmapbitmap= Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvascanvas=newCanvas(bitmap);

layer.setBounds(0, 0, layer.getIntrinsicWidth(), layer.getIntrinsicHeight());
layer.draw(canvas);

returnnewBitmapDrawable(context.getResources(), bitmap);
}

Solution 2:

I had the same issue on pre-lollipop, I solved replacing:

vBroken.getBackground().setColorFilter(backgroundColor, PorterDuff.Mode.SRC);

with:

Drawabled= vBroken.getBackground();
    d.setColorFilter(backgroundColor, PorterDuff.Mode.MULTIPLY);
    vBroken.setBackground(d);

Solution 3:

There's an issue in AppCompat with compound Drawable below API 21 that me thinks is related: https://code.google.com/p/android/issues/detail?id=191111

The simple solution is not using drawables from XML but create them in code and then apply setColorFilter. That's why @Hardeep solution worked.

Fun trivia: In my case setColorFilter on XML-created TextViewdrawableLeft worked fine, but only when invoked via click handler / delayed. When invoked in onCreate / onResume etc nothing happened.

Solution 4:

For me, a ColorFilter would not be applied to a color item in a StateListDrawable.

Creating a Drawable to represent that color and using that instead in the StateListDrawable saw setColorFilter working on pre-Lollipop devices I tested.

As a side note, I made a pure white Drawable so that tinting color would be applied at full opacity.

Solution 5:

Have you tried directly like this?!

vBroken.getBackground().setColorFilter(Color.argb(255, 255, 255, 255),PorterDuff.Mode.MULTIPLY));

Post a Comment for "Setcolorfilter() Broken On Android 4, Working On Android 5"