我在资源文件中有一张图片。
Drawable draw = getResources().getDrawable(R.drawable.my_icon);
图像具有透明背景。
在我的代码中进一步使用最终产品之前,有没有办法以编程方式将背景颜色设置为Drawable?
答案 0 :(得分:0)
我认为使用PorterduffXferMode绘图可能会对您有所帮助。通过这种方式,您可以以多种方式合并两个图像(您的图像和完全覆盖您想要替换透明像素的颜色)。
解释了不同的porterduff模式: http://www.ibm.com/developerworks/java/library/j-mer0918/
Android示例: http://www.vogella.com/code/ApiDemos/src/com/example/android/apis/graphics/Xfermodes.html
这样您可以在新的Bitmap中绘制结果。 (如果您的图像是src并且背景用作dst,则SRC_OVER应该适用于您的情况)
答案 1 :(得分:0)
setColorFilter()
的 SRC
将破坏drawable的透明度。
我在我的代码中使用了它,并且它可以正常工作
disabledIcon = ContextCompat.getDrawable(getContext(), resId);
disabledIcon = DrawableCompat.wrap(disabledIcon);
disabledIcon.mutate(); // to not share its state with any other drawable
DrawableCompat.setTint(disabledIcon, ContextCompat.getColor(getContext(), R.color.button_text_disabled));