How to change colors of a Drawable in Android?

前端 未结 21 1880
野性不改
野性不改 2020-11-22 13:50

I\'m working on an android application, and I have a drawable that I\'m loading up from a source image. On this image, I\'d like to convert all of the white pixels to a dif

相关标签:
21条回答
  • 2020-11-22 14:21

    If you have your drawable set to the ImageView you can do it with a 1 liner:

    yourImageView.setColorFilter(context.getResources().getColor(R.color.YOUR_COLOR_HERE);
    
    0 讨论(0)
  • 2020-11-22 14:23

    I was able to do this with the following code, which is taken from an activity (the layout is a very simple one, just containing an ImageView, and is not posted here).

    private static final int[] FROM_COLOR = new int[]{49, 179, 110};
    private static final int THRESHOLD = 3;
    
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_colors);
    
        ImageView iv = (ImageView) findViewById(R.id.img);
        Drawable d = getResources().getDrawable(RES);
        iv.setImageDrawable(adjust(d));
    }
    
    private Drawable adjust(Drawable d)
    {
        int to = Color.RED;
    
        //Need to copy to ensure that the bitmap is mutable.
        Bitmap src = ((BitmapDrawable) d).getBitmap();
        Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
        for(int x = 0;x < bitmap.getWidth();x++)
            for(int y = 0;y < bitmap.getHeight();y++)
                if(match(bitmap.getPixel(x, y))) 
                    bitmap.setPixel(x, y, to);
    
        return new BitmapDrawable(bitmap);
    }
    
    private boolean match(int pixel)
    {
        //There may be a better way to match, but I wanted to do a comparison ignoring
        //transparency, so I couldn't just do a direct integer compare.
        return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD &&
            Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD &&
            Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
    }
    
    0 讨论(0)
  • 2020-11-22 14:23

    It works for some simple drawables. I used it on a simple solid color rect shape with rounded corners and needed to change that color with different layouts.

    Try this

    android:backgroundTint="#101010"
    
    0 讨论(0)
  • 2020-11-22 14:23

    It's very very simple when you use a library to do that for you. Try this library

    You can call like this:

    Icon.on(holderView).color(R.color.your_color).icon(R.mipmap.your_icon).put();
    
    0 讨论(0)
  • 2020-11-22 14:24

    In your Activity you can tint your PNG image resources with a single colour:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myColorTint();
        setContentView(R.layout.activity_main);
    }
    
    private void myColorTint() {
        int tint = Color.parseColor("#0000FF"); // R.color.blue;
        PorterDuff.Mode mode = PorterDuff.Mode.SRC_ATOP;
        // add your drawable resources you wish to tint to the drawables array...
        int drawables[] = { R.drawable.ic_action_edit, R.drawable.ic_action_refresh };
        for (int id : drawables) {
            Drawable icon = getResources().getDrawable(id);
            icon.setColorFilter(tint,mode);
        }
    }
    

    Now when you use the R.drawable.* it should be coloured with the desired tint. If you need additional colours then you should be able to .mutate() the drawable.

    0 讨论(0)
  • 2020-11-22 14:27

    Short example to change drawable color according to isWorking field.

    My shape xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <solid android:color="@android:color/holo_blue_bright" />
        <corners android:radius="30dp" />
        <size
            android:height="15dp"
            android:width="15dp" />
    </shape>
    

    My method to change:

    private Drawable getColoredDrawable(int drawableResId, boolean isworking) {
        Drawable d = getResources().getDrawable(R.drawable.shape);
        ColorFilter filter = new LightingColorFilter(
                isworking ? Color.GREEN : Color.RED,
                isworking ? Color.GREEN : Color.RED);
        d.setColorFilter(filter);
        return d;
    }
    

    Example of usage:

    text1.setCompoundDrawablesWithIntrinsicBounds(getColoredDrawable(R.drawable.shape, isworking()), null, null, null);
    
    0 讨论(0)
提交回复
热议问题