如何在画布和路径中进行抗锯齿处理

4

在使用canvas.clipPath时,我遇到了一个问题:它显示为锯齿状,看起来不够平滑。我知道如果使用Paint,可以使用mPaint.setFlags(Paint.ANTI_ALIAS_FLAG)来进行抗锯齿处理,但是在我的代码中,我无法使用paint。

public static void drawCurrentPageArea(Canvas canvas, Bitmap bitmap) {
    //cebakhja


    canvas.save();
    canvas.clipPath(getPath5(), Region.Op.XOR);
    canvas.drawBitmap(bitmap, 0, 0, null);
    canvas.restore();
}

public static Path getPath5()
{
    Path mPath5 = new Path();

    mPath5.moveTo(ptc.x, ptc.y);
    mPath5.quadTo(pte.x, pte.y, ptb.x,ptb.y);
    mPath5.lineTo(pta.x, pta.y);
    mPath5.lineTo(ptk.x, ptk.y);
    mPath5.quadTo(pth.x, pth.y, ptj.x,ptj.y);
    mPath5.lineTo(ptf.x, ptf.y);
    mPath5.close();
    return mPath5;
}

您可以看到我使用了canvas.drawBitmap(bitmap, 0, 0, null)的方法,其中paint是null。如果我需要添加一个paint,您有什么建议吗?图片如下:http://i.6.cn/cvbnm/36/5c/20/5d8d20e3bafe432d792793509f99131e.jpg 编辑:我设置了paint为null,但没有效果。

你说你不能在 ANTI_ALIAS_FLAG 中使用 paint,为什么? - SteelBytes
在我的代码中,你可以看到我没有使用 paint,因此我无法使用 ANTI_ALIAS_FLAG。 - pengwang
1
我看到你不这样做,我问为什么不这样做。也就是说,为什么不使用canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG));的方式。 - SteelBytes
我也使用了这种方法,但没有效果。你有其他的方法吗? - pengwang
1个回答

9

试试这个。

private Paint mBitmapPaint = new Paint() {
    {
        setAntiAlias(true);
        setFilterBitmap(true);
    }
};

public static void drawCurrentPageArea(Canvas canvas, Bitmap bitmap) {
    // cebakhja
    canvas.save();
    canvas.clipPath(getPath5(), Region.Op.XOR);
    canvas.drawBitmap(bitmap, 0, 0, mBitmapPaint);
    canvas.restore();
}

+1 - 更好但不完美。有没有其他方法可以平滑裁剪图像的边缘?如果我只需要绘制椭圆形状,我可以使用位图遮罩,但如果我必须将圆形状裁剪到位图中,则不可能这样做。 - schlingel
2
一个想法:你可以尝试使用paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN))在位图上方绘制一条模糊透明的线,宽度为几个像素(具体多少自己决定),或者是DST_OUT模式。 - Renard
这实际上是一个相当聪明的想法。我会尝试一下,谢谢! - schlingel
工作得很好。起初我担心会出现OutOfMemoryErrors,因为我需要使用两个新的位图,但似乎这并不是一个问题。 - schlingel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接