以编程方式更改可绘制的颜色

我试图通过代码更改白色标记图像上的颜色。 我读过下面的代码应该改变颜色,但我的标记保持白色。

Drawable.setColorFilter( 0xffff0000, Mode.MULTIPLY ) 

我错过了什么? 有没有其他的方式来改变位于我res文件夹中的drawables上的颜色?

尝试这个:

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

编辑: getDrawable(int id)在当前版本的Android中已弃用。

使用ContextCompat.getDrawable(context, R.drawable.balloons); 对于这个例子。

你可以试试这个svg矢量绘制

 DrawableCompat.setTint(myImageView.getDrawable(), ContextCompat.getColor(context, R.color.another_nice_color)); 

您可能需要在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); 

另一种方法来做这个在棒棒糖,android 5 + +是设置一个位图的色彩可绘制像这样:

   

如果您想在绘图板上使用有限数量的颜色,这将对您有用。 看看我的博客文章了解更多信息 。

你可以尝试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); 

我已经写了一个通用的函数,您可以通过上下文,图标是id可绘制/ 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); 

你可能想尝试Mode.LIGHTENMode.DARKEN 。 Android Javadocs在解释PorterDuff模式的工作方面非常糟糕。 你可以在这里看看他们: PorterDuff | Android的

我建议在这里浏览 Mozilla网站上的Compositing 。 (他们没有android的所有模式,但他们有很多)

与接受的答案相同,但是更简单的便利方法:

 val myDrawable = ContextCompat.getDrawable(context, R.drawable.my_drawable) myDrawable.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN) setCompoundDrawablesWithIntrinsicBounds(myDrawable, null, null, null) 

请注意,这里的代码是Kotlin。

这对我有效。 确保在0x和颜色代码之间加上“ff”。 像这个0xff2196F3

 Drawable mDrawable = ContextCompat.getDrawable(MainActivity.this,R.drawable.ic_vector_home); mDrawable.setColorFilter(new PorterDuffColorFilter(0xff2196F3,PorterDuff.Mode.SRC_IN)); 

你可以试试这个ImageView。 使用setColorFilter()。

 imageViewIcon.setColorFilter(ContextCompat.getColor(MainActivity.this, R.color.colorWhite)); 

这就是我所做的:

 public static Drawable changeDrawableColor(int drawableRes, int colorRes, Context context) { //Convert drawable res to bitmap final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawableRes); final Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth() - 1, bitmap.getHeight() - 1); final Paint p = new Paint(); final Canvas canvas = new Canvas(resultBitmap); canvas.drawBitmap(resultBitmap, 0, 0, p); //Create new drawable based on bitmap final Drawable drawable = new BitmapDrawable(context.getResources(), resultBitmap); drawable.setColorFilter(new PorterDuffColorFilter(context.getResources().getColor(colorRes), PorterDuff.Mode.MULTIPLY)); return drawable; }