i want to add a color filter to the imageview
Asked Answered
R

5

30

I'd like to add a ColorFilter to ImageView.

Currently I'm using:

ImageView iv = (ImageView)findViewById(resIdOfImageToFilter);
iv.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);

I've checked Multiple Modes in PotterDuff such as SRC_IN, SRC etc., but I'm not getting any difference in any of the modes... All mode turns the whole ImageView in perfect Red color.

I need to blend RED color in the existing image so that image will look with a REDDISH tinge....

Redneck answered 19/11, 2011 at 10:35 Comment(2)
oops ...right way to do is PorterDuff.Mode.Lighten...Redneck
That's because there's no alpha channel (i.e. transparency) in your image. Usually setColorFilter() is used to tint icons, and in fact the default behaviour is to colorize all the image preserving only the alpha channel. PorterDuff.Mode.LIGHTEN preserve brightness instead, so coloured parts of your image turn red, and white ones are left as they are.Rationalism
R
42

The right way to do it was using PorterDuff.Mode.LIGHTEN.

So the updated code will be like:

ImageView iv = (ImageView)findViewById(resIdOfImageToFilter);
iv.setColorFilter(Color.RED, PorterDuff.Mode.LIGHTEN);
Redneck answered 25/1, 2013 at 6:34 Comment(1)
That's because there's no alpha channel (i.e. transparency) in your image. Usually setColorFilter() is used to tint icons, and in fact the default behaviour is to colorize all the image preserving only the alpha channel. PorterDuff.Mode.LIGHTEN preserve brightness instead, so coloured parts of your image turn red, and white ones are left as they are.Rationalism
P
12

You can use android:tint (link) in you xml file. Example:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/your_drawable"
    android:tint="@color/your_color" />
Pliable answered 1/1, 2017 at 7:56 Comment(1)
Should be top answer. Where your_color is something like this; <color name="your_color">#55000000</color>Koerlin
O
9

Other solution, you could have kept PorterDuff.Mode.SRC_ATOP mode and use another alpha to have a transparent color.

I use 155 as Alpha value:

  final int semiTransparentGrey = Color.argb(155, 185, 185, 185);
  drawable.setColorFilter(semiTransparentGrey, PorterDuff.Mode.SRC_ATOP);
Outofdate answered 5/9, 2013 at 15:10 Comment(2)
how do you set 155 aplha?Counselor
You can set alpha directly, setting the first argb value to define the color inside a color.xml resource for example or in the code. Example: int FILTERED_GREY = Color.argb(155, 185, 185, 185);Outofdate
P
8

This worked for me:

in res/colors.xml:

<color name="highlight_color_filter">#A5FF0000</color>

in your Activity initialize the filter and highlight paint:

int highlightColor = context.getResources().getColor(R.color.highlight_color_filter);
PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(highlightColor, PorterDuff.Mode.SRC_ATOP);

Paint redHighLight = new Paint();
redHighLight.setColorFilter(targetHitFilter);
redHighLight.setAlpha(190);

then apply the filter to the ImageView:

ImageView iv=(ImageView)findViewById(ResIdOfImageToFilter);
iv.setColorFilter(redHighLight);

if that doesn't work try applying to the ImageView drawable:

iv.getDrawable().setColorFilter(redHighLight);

hope that helps.

Preach answered 19/11, 2011 at 14:43 Comment(2)
Not to be a meany or anything, but here is a better answer: #10114920Tartarous
This would not compile. redHighLight is-a Paint not a ColorFilter.Ambrosia
P
1

In your xml file you can user tint For example

        <ImageView
            android:id="@+id/scrachImage_1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:tint="@color/colorAccent"
            android:src="@drawable/eagle" />

If You want programmatically add color filter then use

scratchImage_2.setColorFilter(Color.BLACK);

You Can also remove this color filter using this code:

scratchImage_2.setColorFilter(null);

Pervade answered 30/7, 2017 at 5:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.