首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android位图蒙版颜色,删除颜色

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

Stack Overflow用户
提问于 2011-06-22 06:55:21
回答 3查看 23.6K关注 0票数 10

我正在创建位图,接下来我将在它上面绘制第二个纯色位图。现在我想更改第一张位图,这样我在上面绘制的纯色将是透明的。

或者简单地说,我想从位图中删除一种颜色的所有像素。我已经尝试了所有的滤色器和xfermode,但都没有成功,除了逐个像素地移除颜色之外,还有其他的可能性吗?

EN

回答 3

Stack Overflow用户

发布于 2011-06-27 11:18:35

这适用于从位图中删除特定的颜色。主要是AvoidXfermode的使用。如果尝试将一种颜色更改为另一种颜色,它也应该起作用。

我要补充的是,这回答了从位图中删除颜色的问题标题。像OP说的那样,使用PorterDuff Xfermode可能会更好地解决特定的问题。

代码语言:javascript
复制
// 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
票数 13
EN

Stack Overflow用户

发布于 2012-08-21 03:45:43

在API16级(Jelly Bean)之前,user487252's solution就像一个护身符一样工作,在此之后,AvoidXfermode似乎完全不起作用了。

在我的特定用例中,我将PDF的页面(通过APV PDFView)呈现为一个像素数组int[],并将其传递给Bitmap.createBitmap( int[], int, int, Bitmap.Config )。此页面包含绘制到白色背景上的线条艺术,我需要删除背景,同时保留抗锯齿。

我找不到一种Porter-Duff模式来做我想要的事情,所以我最终对像素进行了反复迭代,并逐个对其进行变换。结果出人意料地简单和高效:

代码语言:javascript
复制
int [] pixels = ...;

for( int i = 0; i < pixels.length; i++ ) {
    // Invert the red channel as an alpha bitmask for the desired color.
    pixels[i] = ~( pixels[i] << 8 & 0xFF000000 ) & Color.BLACK;
}

Bitmap bitmap = Bitmap.createBitmap( pixels, width, height, Bitmap.Config.ARGB_8888 );

这对于绘制线条艺术是完美的,因为任何颜色都可以用于线条,而不会失去抗锯齿效果。我在这里使用红色通道,但是您可以通过移位16位而不是8来使用绿色,或者通过移位24来使用蓝色。

票数 4
EN

Stack Overflow用户

发布于 2011-06-22 07:00:36

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6432965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档