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