gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:26:18 24 4
gpt4 key购买 nike

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

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

最佳答案

这适用于从位图中删除某种颜色。主要部分是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

关于Android 位图蒙版颜色,去除颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6432965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com