简体   繁体   中英

Change png icon color in from drawable resources

I have an icon in png in drawable. It's black with the transparent background. How can I change the icon color without adding another drawable?

You can use a ColorFilter to change the icons color at runtime.

Try something like this:

    Drawable mIcon= ContextCompat.getDrawable(getActivity(), R.drawable.your_icon);
    mIcon.setColorFilter(ContextCompat.getColor(getActivity(), R.color.new_color), PorterDuff.Mode.MULTIPLY);
    mImageView.setImageDrawable(mIcon);

A good helper function for this is,

public Drawable getTintedDrawable(Resources res,
    @DrawableRes int drawableResId, @ColorRes int colorResId) {
    Drawable drawable = res.getDrawable(drawableResId);
    int color = res.getColor(colorResId);
    drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    return drawable;
}

Fast Android asset theming with ColorFilter - Dan Lew

you can try this

Drawable mDrawable = context.getResources().getDrawable(R.drawable.yourdrawable); 
mDrawable.setColorFilter(new 
PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));

Try using this static method:

public static Drawable changeDrawableColor(Drawable drawable, int color) {
    drawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, color);
    return drawable;
}

The color parameter could be a color from your resources.

Drawable mDrawable = context.getResources().getDrawable(R.drawable.balloons); 
mDrawable.setColorFilter(new 
PorterDuffColorFilter(0xffff00,PorterDuff.Mode.LIGHTEN));

Try the above You can Play with PorterDuffColorFilter(0xffff00,PorterDuff.Mode.LIGHTEN) You can use Black etc.

PorterDuff.Mode.SRC_IN

Using this property you change the color of the icon with the exact color chosen.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        Drawable mIcon= ContextCompat.getDrawable(this, R.drawable.icon_send);
        mIcon.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
        ibSendMessage.setBackground(mIcon);
}

In newer versions of Android you can do this in XML

android:backgroundTint="@color/colorAccent"

If you change "Drawable" other 'ImageView's that use this "Drawable resource" will also change, it is better to apply the filter to single "ImageView".

Using these two functions, you can enter the color you want as an ID (like: R.color.white) or color code (like: #efec0c).

public void ChangePngIconColor(String Target_Color, ImageView Target_ImageView){
    
    /*
     * Sample: 
     * Target_Color = "#efec0c"; OR Target_Color = "efec0c";
     * 
     */
    
    Target_Color = (Target_Color.startsWith("#")) ? Target_Color : "#"+Target_Color;
    
    Target_ImageView.setColorFilter(Color.parseColor(Target_Color), PorterDuff.Mode.SRC_IN);
}


public void ChangePngIconColor(int Target_Color_ID, ImageView Target_ImageView){
    
    /*
     * Sample: Target_Color = R.color.white;
     * 
     */
    
    Target_ImageView.setColorFilter(ContextCompat.getColor(context,Target_Color_ID), PorterDuff.Mode.SRC_IN);
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM