Android:在画布上绘制带有一些颜色的透明位图



它对我来说很头疼,我有一个透明的png图像,,, 我已将其解码为位图,然后添加到画布上,

BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), picList[0] , options);
Paint p = new Paint();      
canvas.drawBitmap(mBitmap, 0, 0, p);

稍后当我保存画布时,它显示图像但将背景显示为黑色,为了实现白色,我使用了一些代码,例如

Paint p = new Paint();
p.setAlpha(color.white);
p.setColor(color.white);
canvas.drawColor(color.white);
canvas.drawPaint(p);

但颜色并没有定为白色,,, 请帮帮我,,我希望背景保存的图像是白色的。如果还有其他逻辑,我错过了。谢谢你帮忙。

您可以在

画布上绘制后在位图上调用以下内容:

        for(int x=0;x<bitmap.getWidth();x++){
            for(int y=0;y<bitmap.getHeight();y++){
                if(bitmap.getPixel(x, y)==Color.BLACK){
                    bitmap.setPixel(x, y, Color.WHITE);
                }
            }
        }

如果您希望保存的图像使用白色bg,请使用WHITE,否则您可以使用TRANSPARENT。

好吧,

我已经找到了正确的选择。其 canvas.drawARGB(255,30,30,39);给出各种 ARGB 值并在画布上获取颜色享受:D

在创建 Paint 对象之前,请调用 canvas.drawColor(color.white); 如果这不起作用,请使用canvas.drawColor(Color.WHITE,PorterDuff.Mode.DARKEN);

您可以尝试使用 Color.TRANSPARENT 而不是 Color.white。

很好!

如果你的图像有坏的白色(不是真的(255 255 255)),你可以考虑容忍度

for(int x=0;x<img.getWidth();x++)
{
    for(int y=0;y<img.getHeight();y++)
    {
        int cou = img.getPixel(x, y), tolerancy = 40//max 255, your image would be completely tranpsparent;
        if(Math.abs(Color.red(cou)-255)<tolerancy && 
           Math.abs(Color.green(cou)-255)<tolerancy &&  
           Math.abs(Color.blue(cou)-255)<tolerancy)
            paletteFond.setPixel(x, y, Color.TRANSPARENT);
    }
}

最新更新