从可绘制资源中更改 png 图标颜色
Change png icon color in from drawable resources
我在可绘制文件中有一个 png 格式的图标。它是黑色的,背景是透明的。如何在不添加其他可绘制对象的情况下更改图标颜色?
你可以试试这个
Drawable mDrawable = context.getResources().getDrawable(R.drawable.yourdrawable);
mDrawable.setColorFilter(new
PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));
您可以使用 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);
Drawable mDrawable = context.getResources().getDrawable(R.drawable.balloons);
mDrawable.setColorFilter(new
PorterDuffColorFilter(0xffff00,PorterDuff.Mode.LIGHTEN));
试试上面的 你可以玩 PorterDuffColorFilter(0xffff00,PorterDuff.Mode.LIGHTEN)
可以用黑色等
尝试使用这个静态方法:
public static Drawable changeDrawableColor(Drawable drawable, int color) {
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, color);
return drawable;
}
颜色参数可以是您资源中的颜色。
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"
一个很好的辅助函数是,
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;
}
如果您更改“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);
}
我在可绘制文件中有一个 png 格式的图标。它是黑色的,背景是透明的。如何在不添加其他可绘制对象的情况下更改图标颜色?
你可以试试这个
Drawable mDrawable = context.getResources().getDrawable(R.drawable.yourdrawable);
mDrawable.setColorFilter(new
PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));
您可以使用 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);
Drawable mDrawable = context.getResources().getDrawable(R.drawable.balloons);
mDrawable.setColorFilter(new
PorterDuffColorFilter(0xffff00,PorterDuff.Mode.LIGHTEN));
试试上面的 你可以玩 PorterDuffColorFilter(0xffff00,PorterDuff.Mode.LIGHTEN) 可以用黑色等
尝试使用这个静态方法:
public static Drawable changeDrawableColor(Drawable drawable, int color) {
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, color);
return drawable;
}
颜色参数可以是您资源中的颜色。
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"
一个很好的辅助函数是,
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;
}
如果您更改“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);
}