Android位图蒙版颜色,删除颜色

ZZZ*_*ZZZ 10 android alpha mask bitmap

我正在创建位图,接下来我在它上面绘制第二个纯色位图.现在我想改变第一个位图,所以我在它上面绘制的纯色将是透明的.

或者简单地说,我想从位图中删除一种颜色的所有像素.我havie试过每个colorfilter,xfermode没有运气,还有其他可能去除颜色,而不是像素一样吗?

小智 13

这适用于从位图中删除某种颜色.主要部分是使用AvoidXfermode.如果尝试将一种颜色更改为另一种颜色,它也应该有效.

我应该补充说,这回答了从位图中删除颜色的问题标题.如OP所说,使用PorterDuff Xfermode可能更好地解决了具体问题.

// 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
Run Code Online (Sandbox Code Playgroud)


Ric*_*ich 0

逐个像素并不是一个坏的选择。只是不要在循环内调用 setPixel 。使用 getPixels 填充 argb int 数组,如果不需要保留原始值,则就地修改它,然后在最后调用 setPixels。如果内存是一个问题,您可以逐行执行此操作,或者您可以一次完成整个操作。您不需要为覆盖颜色填充整个位图,因为您只需进行简单的替换(如果当前像素为 color1,则设置为 color2)。