Android Mask两个Bitmap清晰边框

时间:2012-12-23 00:46:26

标签: android bitmap masking

我正在使用掩码和另一个位图。操作成功很好,不幸的是,屏蔽的结果看到了一个轻微的黑色边框,如图所示:

enter image description here

如何删除此边框?在源图像中不存在。

我会发布我正在使用的代码:

public Bitmap mask(Bitmap source) {
    Bitmap targetBitmap = Bitmap.createBitmap(getWidth(),getHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(targetBitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    paint.setAntiAlias(true);
    paint.setDither(true);
    canvas.drawBitmap(source, 0, 0, null);
    canvas.drawBitmap(getMask(), 0, 0, paint);
    paint.setXfermode(null);
    return targetBitmap;        
}

其中getMask()返回表示Puzzle图形的Bitmap。 我希望得到你的帮助,谢谢大家

对不起我的英文: - )

更新:

黑色边框是我在这张照片中指出的:

enter image description here

更新:

放置转换序列。第三个图像与第一个图像相同但没有颜色。问题是拼图的黑色边缘。 我希望更清楚:

enter image description here

1 个答案:

答案 0 :(得分:0)

我用面具绘制图像的方式与你所做的相反。

public Bitmap mask(Bitmap source) {
    Bitmap targetBitmap = Bitmap.createBitmap(getWidth(),getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(targetBitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    // paint.setAntiAlias(true); // you've already set this in the constructor
    paint.setDither(true);
    canvas.drawBitmap(getMask(), 0, 0, null);
    canvas.drawBitmap(source, 0, 0, paint);
    // paint.setXfermode(null); // no need for this
    return targetBitmap;        
}

请注意,PorterDuff.Mode设置为SRC_IN(而不是DST_in),并且首先绘制蒙版,然后绘制该蒙版顶部的图像。使用这种方法,您还可以绘制前一个源作为基本蒙版,添加新的(拼图)蒙版,然后使用SRC_IN绘制在其上绘制最终源/图像,以便每次添加新的拼图块。 如果这不能解决黑色边框,请检查您的蒙版是否没有可能导致这些问题的羽状(透明)边缘。

此外,ANTI_ALIAS_FLAG对纹理没有任何作用。如果你想要平滑缩放的纹理,请使用paint.setFilterBitmap(true);