我正在尝试通过代码更改白色标记图像上的颜色。我读到下面的代码应该会改变颜色,但我的标记仍然是白色的。
Drawable.setColorFilter( 0xffff0000, Mode.MULTIPLY )我错过了什么吗?有没有其他方法可以改变我的res文件夹中的绘图文件的颜色?
发布于 2012-07-08 00:12:04
试试这个:
Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.my_drawable);
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, Color.RED); 使用DrawableCompat很重要,因为它在API22和更早版本的设备上提供了向后兼容性和错误修复。
发布于 2016-10-06 17:40:44
您可以在svg矢量可绘制中尝试此方法
DrawableCompat.setTint(
DrawableCompat.wrap(myImageView.getDrawable()),
ContextCompat.getColor(context, R.color.another_nice_color)
);发布于 2016-01-21 15:11:06
您可能需要在可绘制对象上调用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);https://stackoverflow.com/questions/11376516
复制相似问题