setColorFilter not working

后端 未结 10 1442
春和景丽
春和景丽 2020-12-05 12:43

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:

    we         


        
相关标签:
10条回答
  • 2020-12-05 13:18

    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);
    
    0 讨论(0)
  • 2020-12-05 13:19

    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.

    0 讨论(0)
  • 2020-12-05 13:24

    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 :)

    0 讨论(0)
  • 2020-12-05 13:25

    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

    0 讨论(0)
  • 2020-12-05 13:27

    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);
    
    0 讨论(0)
  • 2020-12-05 13:31

    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.

    0 讨论(0)
提交回复
热议问题