Android change color of ImageView / Bitmap
Asked Answered
A

6

8

I need to find a way to change the color of bitmap in Android. I need to replace/change colors of oval image smoothly in my application depending on int value. I need something like if myValue=5 than change my image's color to RED and if myValue=322 change color to BLUE. The only way which I find I can do this was using xml file which looks like this :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
 <solid android:color="#cccccc"/> 
    <corners
     android:bottomRightRadius="10dp"
     android:bottomLeftRadius="10dp"
     android:topLeftRadius="10dp"
     android:topRightRadius="10dp"/>
</shape>

and after that when myValue is changing to set my ImageView image resource. But in this way I have to create 35 different xml files...which I don't think is a good idea.

So anyone who can suggest better solution to do this?

Ama answered 4/5, 2012 at 13:34 Comment(0)
A
21

This is how I solved this issue :

  1. Declare an ImageView with src="@drawable/button"
  2. Create a Drawable and set ColorFilter to it and after that use it as src to your declared ImageView like this :

>

Drawable myIcon = getResources().getDrawable( R.drawable.button );
ColorFilter filter = new LightingColorFilter( Color.BLUE, Color.BLUE );
myIcon.setColorFilter(filter);
color.setImageDrawable(myIcon);
Ama answered 7/5, 2012 at 14:19 Comment(2)
Can you also mention how to do this in the xml if its possible?Maice
Nvm I found it, have to use the the android:tint attribute for imageviewMaice
C
16

This solution doesn't work very well for me. In some images the final color was wrong. I use this solution instead:

Drawable myIcon = getResources().getDrawable(R.drawable.your_image); 
myIcon.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP); 
((ImageView)findViewById(R.id.view_to_change)).setImageDrawable(myIcon);
Conformist answered 24/1, 2014 at 21:23 Comment(0)
S
3
getResources().getDrawable( R.drawable.button );

is now deprecated. Can also do it this way:

((ImageView) findViewById(R.id.my_icon))
  .setColorFilter(new LightingColorFilter(Color.BLUE, Color.BLUE));
Spathe answered 22/10, 2015 at 1:17 Comment(0)
P
1

Should you this.

Drawable myIcon = getResources().getDrawable( R.drawable.button ); 
ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK);
myIcon.setColorFilter(filter);
Pastoralize answered 7/3, 2013 at 8:52 Comment(0)
A
0

You can use a TransitionDrawable to achieve this - http://developer.android.com/reference/android/graphics/drawable/TransitionDrawable.html

Asparagine answered 4/5, 2012 at 13:40 Comment(2)
I saw that option but didn't find any good example or explanation how to use it.Ama
#2615045Asparagine
C
0

Try this:

private final ImageView uiIV_statusIcon;
uiIV_statusIcon = anyView.findViewById(R.id.iv_status);
uiIV_statusIcon.setImageResource(R.drawable.ic_twotone_error_24);

in Activity:

ImageViewCompat.setImageTintList(uiIV_statusIcon, ColorStateList.valueOf(getColor(R.color.md_red_400)));

in a Fragment

ImageViewCompat.setImageTintList(uiIV_statusIcon, ColorStateList.valueOf(getContext().getColor(R.color.md_red_400)));

in a Recyclerviewadapter with preassigned variable _context :

ImageViewCompat.setImageTintList(uiIV_statusIcon, ColorStateList.valueOf(_context.getColor(R.color.md_red_400)));
Cagliari answered 7/10, 2021 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.