43

I am doing such type of project ,In my project change Image color dynamically.

I have a one black shape color image ,when user click on this image change image color dynamically green.

enter image description here

Googling and other document follow but I am not solve my problem .

Please help me , is there any method or document to follow solve my problem ,

Hemantvc
  • 2,111
  • 3
  • 30
  • 42
  • I have only one single black color shape image .dynamically color change that image. – Hemantvc Jan 08 '13 at 04:51
  • Try,to understand already discussed in stack overflow post's and find the links below: http://stackoverflow.com/questions/5264706/how-to-replace-color-of-an-image http://stackoverflow.com/questions/23763/colorizing-images-in-java and below links are external find this, http://forum.intern0t.org/java-ruby/3932-java-source-change-image-color.html http://www.javalobby.org/articles/ultimate-image/ http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/RGBImageFilter.html http://www.jhlabs.com/ip/filters/ – Dinesh Jan 08 '13 at 05:00
  • I solve my issue using http://www.41post.com/4396/programming/android-bitmap-to-integer-array – Hemantvc Nov 19 '15 at 10:08
  • Your solution is [here](http://stackoverflow.com/a/27384651/4632372). Hope it solves your issue. – Mohammedsalim Shivani Mar 27 '17 at 10:06
  • Please have a look at [here](http://stackoverflow.com/a/27384651/4632372). I hope it helps. Thank You – Mohammedsalim Shivani Mar 27 '17 at 10:08

6 Answers6

57

Here's how I do this: It's pulling the color from a resource xml file.

<resources>
<color name="new_color">#FFAAAAAA</color>
</resources>

In your activity .java file:

import android.graphics.PorterDuff.Mode;

Resources res = context.getResources();
final ImageView image = (ImageView) findViewById(R.id.imageId);
final int newColor = res.getColor(R.color.new_color);
image.setColorFilter(newColor, Mode.SRC_ATOP);

To clear it call:

image.setColorFilter(null);
Chuck D
  • 1,629
  • 2
  • 16
  • 32
  • thank you for replying ,i am try code but not solve my problem ,this code change whole image color not image shape color – Hemantvc Jan 08 '13 at 05:18
  • Try a diffferent Mode and it will work for your example: http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html – Chuck D Jan 08 '13 at 05:20
  • I am try different mode ,this was change image color not image shape color . – Hemantvc Jan 08 '13 at 05:42
  • Yea, maybe OVERLAY, SATURATE, SCREEN. If you can use an alpha mask on the white part and use a white background in the parent view, that might work as well. – Chuck D Jan 08 '13 at 05:53
  • try this mode but i am not able to change image shape color. – Hemantvc Jan 08 '13 at 05:58
  • res.getColor() is now depreciated. This worked instead though: ContextCompat.getColor(context, R.color.my_color) – Chris Dutrow Apr 18 '17 at 02:48
  • 1
    This is better than `android:tint` because it's more general. `android:tint` assumes `PorterDuff.Mode#SRC_ATOP` according to https://developer.android.com/reference/android/widget/ImageView#setColorFilter(int) – Michael Osofsky Jul 07 '20 at 23:00
27

Set android:tint attribute of image/image button to the color you need.

android:tint="@android:color/black"

Optionally you can set android:tintMode attribute.

Borzh
  • 5,069
  • 2
  • 48
  • 64
16
imageView.setImageResource(R.drawable.ic_person_black_48dp);

imageView.setColorFilter(imageView.getContext().getResources().getColor(R.color.desired_color), PorterDuff.Mode.SRC_ATOP);
Ankit Aman
  • 999
  • 6
  • 15
11

Create resource in drawable folder like

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:src="@drawable/rect"
            android:tint="@color/red"/>
    </item>
</layer-list>

android:tint="@color/red" does it.

cakan
  • 2,099
  • 5
  • 32
  • 42
Metro Polinet
  • 132
  • 1
  • 3
3

In XML use src not background tag in ImageView. In java code-

import android.graphics.PorterDuff.Mode;
final Context context=this;

        home1=(ImageView) findViewById(R.id.home1);
 Resources res = context1.getResources();
        final int newColor = res.getColor(android.R.color.black);
        home1.setColorFilter(newColor, Mode.SRC_ATOP);
Ankit Gupta
  • 139
  • 1
  • 11
0

Put this in your OnDraw, just before you draw your square.

if (userclicked){
paint.setColor(Color.GREEN);
} else {
paint.setColor(Color.BLACK);
}

Of course that is if you are drawing it with canvas.drawRect(x0,y0,x1,y1,paint) which you would if you were drawing a simple shape like that.

Jake
  • 132
  • 2
  • 7
  • sorry this is not my problem ,i have a black color shape image and that image color change dynamically. – Hemantvc Jan 08 '13 at 04:55