Android位图遮罩颜色,去除颜色 [英] Android bitmap mask color, remove color

查看:23
本文介绍了Android位图遮罩颜色,去除颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

或者简单地说,我想从位图中删除一种颜色的所有像素.我已经尝试了所有滤色器和 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.

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆