Android的位图掩盖颜色,颜色删除 [英] Android bitmap mask color, remove color
问题描述
我创建位图,然后我在画在它上面的第二纯色位图。
现在我想改变第一个位图,让纯色,我drawed上这将是透明的。
I am creating bitmap, next i am drawing second solid color bitmap on top of it. And now i want to change first bitmap, so solid color that i drawed on it will be transparent.
或者干脆,我想从位图中删除一个颜色的所有像素。
我havie想尽colorfilter,并xfermode没有运气,有没有其他的可能性,以消除其他颜色由像素做像素?
Or simply, i want to remove all pixels of one color from bitmap. I havie tried every colorfilter, and xfermode with no luck, is there any other possibility to remove color other that doing it pixel by pixel?
推荐答案
这适用于从位图删除某种颜色。其主要部分是使用AvoidXfermode的。还应该工作,如果试图以一种颜色变成另一种颜色。
This works for removing a certain color from a bitmap. The main part is the use of AvoidXfermode. It should also work if trying to change one color to another color.
我要补充,这回答移除从位图颜色的问题称号。具体的问题是使用PorterDuff Xfermode像OP说可能是更好的解决。
I should add that this answers the question title of removing a color from a bitmap. The specific question is probably better solved using PorterDuff Xfermode like the OP said.
// start with a Bitmap bmp
// make a mutable copy and a canvas from this mutable bitmap
Bitmap mb = bmp.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(mb);
// get the int for the colour which needs to be removed
Paint p = new Paint();
p.setARGB(255, 255, 0, 0); // ARGB for the color, in this case red
int removeColor = p.getColor(); // store this color's int for later use
// Next, set the alpha of the paint to transparent so the color can be removed.
// This could also be non-transparent and be used to turn one color into another color
p.setAlpha(0);
// then, set the Xfermode of the pain to AvoidXfermode
// removeColor is the color that will be replaced with the pain't color
// 0 is the tolerance (in this case, only the color to be removed is targetted)
// Mode.TARGET means pixels with color the same as removeColor are drawn on
p.setXfermode(new AvoidXfermode(removeColor, 0, AvoidXfermode.Mode.TARGET));
// draw transparent on the "brown" pixels
c.drawPaint(p);
// mb should now have transparent pixels where they were red before
这篇关于Android的位图掩盖颜色,颜色删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!