我正在尝试通过代码更改白色标记图像上的颜色.我已经读过下面的代码应该改变颜色,但我的标记仍然是白色的.
Drawable.setColorFilter( 0xffff0000, Mode.MULTIPLY )
Run Code Online (Sandbox Code Playgroud)
我错过了什么?有没有其他方法可以改变位于我的res文件夹中的drawables上的颜色?
ρяσ*_*я K 215
试试这个:
Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.my_drawable);
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, Color.RED);
Run Code Online (Sandbox Code Playgroud)
编辑:DrawableCompat
在当前版本的Android中已弃用.
使用DrawableCompat
这个例子.
amo*_*new 110
您可以尝试使用svg vector drawable
DrawableCompat.setTint(
DrawableCompat.wrap(myImageView.getDrawable()),
ContextCompat.getColor(context, R.color.another_nice_color)
);
Run Code Online (Sandbox Code Playgroud)
Min*_*Man 20
在Lollipop上执行此操作的另一种方法是,android 5. +在这样的位图可绘制上设置一个色调:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_back"
android:tint="@color/red_tint"/>
Run Code Online (Sandbox Code Playgroud)
如果您要在drawables上使用有限数量的颜色,这将适合您.查看我的博客文章了解更多信息.
shi*_*cky 20
您可能需要在drawable上调用mutate(),否则所有图标都会受到影响.
Drawable icon = ContextCompat.getDrawable(getContext(), R.drawable.ic_my_icon).mutate();
TypedValue typedValue = new TypedValue();
getContext().getTheme().resolveAttribute(R.attr.colorIcon, typedValue, true);
icon.setColorFilter(typedValue.data, PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用ColorMatrixColorFilter,因为您的键颜色为白色:
// Assuming "color" is your target color
float r = Color.red(color) / 255f;
float g = Color.green(color) / 255f;
float b = Color.blue(color) / 255f;
ColorMatrix cm = new ColorMatrix(new float[] {
// Change red channel
r, 0, 0, 0, 0,
// Change green channel
0, g, 0, 0, 0,
// Change blue channel
0, 0, b, 0, 0,
// Keep alpha channel
0, 0, 0, 1, 0,
});
ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);
myDrawable.setColorFilter(cf);
Run Code Online (Sandbox Code Playgroud)
小智 9
你可以尝试这个ImageView
.使用setColorFilter()
.
imageViewIcon.setColorFilter(ContextCompat.getColor(MainActivity.this, R.color.colorWhite));
Run Code Online (Sandbox Code Playgroud)
我写了一个通用函数,你可以在其中传递上下文,图标是id drawable/mipmap图标图标和该图标所需的新颜色.
此函数返回一个drawable.
public static Drawable changeDrawableColor(Context context,int icon, int newColor) {
Drawable mDrawable = ContextCompat.getDrawable(context, icon).mutate();
mDrawable.setColorFilter(new PorterDuffColorFilter(newColor, PorterDuff.Mode.SRC_IN));
return mDrawable;
}
changeDrawableColor(getContext(),R.mipmap.ic_action_tune, Color.WHITE);
Run Code Online (Sandbox Code Playgroud)
与接受的答案相同,但更简单的便捷方法:
val myDrawable = ContextCompat.getDrawable(context, R.drawable.my_drawable)
myDrawable.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN)
setCompoundDrawablesWithIntrinsicBounds(myDrawable, null, null, null)
Run Code Online (Sandbox Code Playgroud)
注意,这里的代码是 Kotlin。
这对我有用。确保在0x和颜色代码之间放置“ ff”。赞这个0xff2196F3
Drawable mDrawable = ContextCompat.getDrawable(MainActivity.this,R.drawable.ic_vector_home);
mDrawable.setColorFilter(new
PorterDuffColorFilter(0xff2196F3,PorterDuff.Mode.SRC_IN));
Run Code Online (Sandbox Code Playgroud)
对于那些使用 Kotlin 的人来说,有一个简单的扩展函数:
fun Drawable.tint(context: Context, @ColorRes color: Int) {
DrawableCompat.setTint(this, context.resources.getColor(color, context.theme))
}
Run Code Online (Sandbox Code Playgroud)
然后简单地做
background.tint(context, R.color.colorPrimary)
Run Code Online (Sandbox Code Playgroud)
使用这个:对于java
view.getBackground().setColorFilter(Color.parseColor("#343434"), PorterDuff.Mode.SRC_OVER)
Run Code Online (Sandbox Code Playgroud)
对于科特林
view.background.setColorFilter(Color.parseColor("#343434"),PorterDuff.Mode.SRC_OVER)
Run Code Online (Sandbox Code Playgroud)
如果您的背景有圆角等,您可以使用 PorterDuff.Mode.SRC_ATOP。