从可绘制资源中更改png图标的颜色

4

我有一个在drawable中的png图标。它是黑色的,背景透明。如何在不添加其他drawable的情况下更改图标颜色?


去绘制并反转它。 - Haroon
没有paint))))),仅适用于Android。 - Igor
请参考以下链接:https://dev59.com/w3M_5IYBdhLWcg3wmkUK#5837189 - Saurabh Vardani
可能是如何在Android中更改Drawable的颜色?的重复问题。 - Jonathan Darryl
7个回答

9
您可以使用ColorFilter在运行时更改图标的颜色。
尝试类似这样的内容:
    Drawable mIcon= ContextCompat.getDrawable(getActivity(), R.drawable.your_icon);
    mIcon.setColorFilter(ContextCompat.getColor(getActivity(), R.color.new_color), PorterDuff.Mode.MULTIPLY);
    mImageView.setImageDrawable(mIcon);

5
一个很好的辅助函数是,
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;
}

使用ColorFilter快速为Android资产进行主题设置 - Dan Lew


2

尝试使用这个静态方法:

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

color参数可以是来自您资源中的颜色。


1

PorterDuff.Mode.SRC_IN

使用这个属性,您可以使用所选的确切颜色更改图标的颜色。
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);
}

在较新版本的Android中,您可以在XML中完成此操作。
android:backgroundTint="@color/colorAccent"

backgroundTint 对我没用,目前我正在使用 Android Studio 4.0。 - syed dastagir

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

尝试使用上述代码,您可以使用PorterDuffColorFilter(0xffff00,PorterDuff.Mode.LIGHTEN)进行操作 您也可以使用黑色等其他颜色。


1
如果更改“Drawable”,使用此“Drawable资源”的其他“ImageView”也会更改,最好将过滤器应用于单个“ImageView”。
使用这两个函数,您可以将想要的颜色作为ID(如:R.color.white)或颜色代码(如:#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);
    
}

0
这是一个在Resources上的Kotlin扩展函数,没有过时的问题。
fun Resources.tintedDrawable(
    @DrawableRes drawableResId: Int, @ColorRes colorResId: Int
): Drawable? {
    val drawable = ResourcesCompat.getDrawable(this, drawableResId, null)
    val color = ResourcesCompat.getColor(this, colorResId, null)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
        drawable?.colorFilter = BlendModeColorFilter(color, BlendMode.SRC_IN)
    else
        drawable?.setColorFilter(color, PorterDuff.Mode.SRC_IN)
    return drawable
}

在活动中的使用方法:
resources.tintedDrawable(R.drawable.my_drawable, R.color.my_tint)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接