setColorFilter not working

AndroidImageviewColorfilter

Android Problem Overview


I'm trying to implement a simple colorfilter on an imageview to turn the black image into a white image. In order to achieve that I do the following:

    weatherImg.setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
    weatherImg.setImageResource(R.drawable.b_clouded_rain);

I've also tried to change to color in the color filter to red and white but all of them have no effect, what am I doing wrong?

Android Solutions


Solution 1 - Android

As much as I hate to answer my own questions I found the problem: I should've used:

   weatherImg.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);

Solution 2 - Android

It depends on what kind of filtering you want to apply. If yout want to apply a new color on an image with transparencies on it, that's what worked for me:

weatherImg.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);

If you want to learn more about this PorterDuff filters, I found a goog article that helped me understand: http://www.ibm.com/developerworks/java/library/j-mer0918/ give it a read :)

Solution 3 - Android

Had the same issue on Android 6. Solved by using ImageView.getDrawable().setColorFilter() instead of ImageView.setColorFilter().

Solution 4 - Android

We use this code

Drawable drawable = DrawableCompat.wrap(getDrawable(drawableResource));
        drawable.mutate();
        DrawableCompat.setTint(drawable, getColor(color));
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

public static int getColor(int id) {
        return ContextCompat.getColor(getInstance().context, id);
    }

public static Drawable getDrawable(int id) {
        return ContextCompat.getDrawable(getInstance().context, id);
    }

Solution 5 - Android

For me, simply calling setColorFilter() on the ImageView wasn't working.

imageView.setColorFilter(ResourcesCompat.getColor(resources, color, null)) //didnt work on 21, only 22+

For whatever reason, on API 21, the only way I could get setColorFilter() to work properly was to post the change to the views message queue.

imageView.post { imageView.setColorFilter(ResourcesCompat.getColor(resources, color, null)) } //this works on 21+

Solution 6 - Android

I was having issues with setColorFilter on a Samsung S3 running 4.3 and the only way I could get the filter to work was by applying it in the draw(Canvas canvas) method:

public class ColouredDrawable extends BitmapDrawable {

private ColorFilter mColorFilter;

public ColouredDrawable(Bitmap toTransform, int toColour, Resources resources) {
    super(resources, toTransform);
    float[] matrix = {
            0, 0, 0, 0, ((toColour & 0xFF0000) / 0xFFFF),
            0, 0, 0, 0, ((toColour & 0xFF00) / 0xFF),
            0, 0, 0, 0, (toColour & 0xFF),
            0, 0, 0, 1, 0 };
    mColorFilter = new ColorMatrixColorFilter(matrix);
}

@Override
public void draw(Canvas canvas) {
    setColorFilter(mColorFilter);
    super.draw(canvas);
}

Simple applying setColorFilter to the BitmapDrawable didn't seem to have any effect.

Solution 7 - Android

I run into the same issue when using multiply

weatherImg.setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);

This worked for png icons but not for vector graphics. Changing it to the default works for both:

 weatherImg.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);

Solution 8 - Android

For me only this solution worked:

image.setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
image.setImageResource(R.drawable.img);

filter applies if R.drawable.img is vector image, and has no effect for raster resource

Solution 9 - Android

For Android 4.3 and 4.4, setColorFilter does not work. Use DrawableCompat instead.

    val drawable = DrawableCompat.wrap(ContextCompat.getDrawable(
            context,
            R.drawable.b_clouded_rain));
    DrawableCompat.setTint(drawable, foregroundColor);
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN)
            .setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);

    weatherImg.setImageResource(R.drawable.b_clouded_rain);

Solution 10 - Android

I might have not the exact problem as yours and yet I think they similar when I try to change a color that not part of the predefined color such as YELLOW>BLUE>GRAY and just a few others, in ColorFiler I was getting the same result no matter what color I tried to use.

So when I used ....

imageView3.setColorFilter(0x85ffdd , PorterDuff.Mode.MULTIPLY);

I was getting the same black color Then I found

int color = Color.parseColor("#85ffdd"); 

now I have no problem to use any colors for my needs.

imageView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int color = Color.parseColor("#85ffdd");
                imageView3.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
                //get other images to default
                imageView1.clearColorFilter();
                imageView2.clearColorFilter();
            }
        });

I hope it might help you or others.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTim KranenView Question on Stackoverflow
Solution 1 - AndroidTim KranenView Answer on Stackoverflow
Solution 2 - AndroidjsideraView Answer on Stackoverflow
Solution 3 - AndroidKonstantin KonopkoView Answer on Stackoverflow
Solution 4 - AndroidRafaelView Answer on Stackoverflow
Solution 5 - AndroidtylerView Answer on Stackoverflow
Solution 6 - Androiddr_sulliView Answer on Stackoverflow
Solution 7 - AndroideugstmanView Answer on Stackoverflow
Solution 8 - AndroidadrayView Answer on Stackoverflow
Solution 9 - AndroidmethodsignatureView Answer on Stackoverflow
Solution 10 - AndroidValery LavrovView Answer on Stackoverflow