Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Completely change Android ImageView's color with tint color

Is there a way to completely change the color of an ImageView in Android? I tried using

imageView.getDrawable().setColorFilter(new  PorterDuffColorFilter(0xFFF7962F, PorterDuff.Mode.MULTIPLY));

and

imageView.setColorFilter(0xFFDDDDDD, PorterDuff.Mode.MULTIPLY);

on an ImageView with a gray image drawable but they only added a color on top of the existing gray making it look kind of weird.

On iOS you can do

[imageView setImage:[[UIImage imageNamed:@"my_image"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
imageView.tintColor = [UIColor redColor];

And it will completely remove whatever color was used to render the UIImageView and replace the "skeleton" image with the designated color. Perhaps it's because the tint always has an alpha transparency on it? I did specify 0xFF on the alpha part though.

like image 487
Math is Hard Avatar asked Jan 09 '23 08:01

Math is Hard


1 Answers

You're using the wrong blend mode, MULTIPLY combines both source colour and the colour you're adding. You probably want this:

imageView.setColorFilter(0xFFDDDDDD, PorterDuff.Mode.SRC_IN);

like image 115
Carl Fletcher Avatar answered Feb 11 '23 17:02

Carl Fletcher