PorterDuff和路径 [英] PorterDuff and Path

查看:123
本文介绍了PorterDuff和路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个位图填充了整个屏幕。在此位图上,我使用

In my project I have a bitmap filling the entire screen. On this bitmap i draw a path with

android.graphics.Canvas.drawPath(Path path, Paint paint)

设置颜料是为了描边并填充路径的内容。我要实现的是擦除与路径相交的位放大部分。我设法在另一个bitmmap而不是路径上使用了porter duff规则,获得了相同的行为。

the paint is set in order to stroke and fill the content of the path. What I would achieve is to erase the portion of the bitamp that intersect the path. I have managed to obtain the same behavior using on another bitmmap instead of the path, and using the porter duff rules. Is there any chance to do the same thing with the path?

    mPaintPath.setARGB(100, 100, 100, 100);// (100, 100, 100, 100)
    mPaintPath.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaintPath.setAntiAlias(true);
    mPath.moveTo(x0, y0));
    mPath.lineTo(x1, y1);
    mPath.lineTo(x2, y2);
    mPath.lineTo(x3, y3);
    mPath.lineTo(x0, y0);
    mPath.close();
    c.drawPath(mPath, mPaintPath);


推荐答案

当然,只需绘制路径到屏幕外缓冲区即可您可以在绘制位图时将其用作遮罩,例如:

Sure, just draw the path to an offscreen buffer so you can use it as a mask when you draw the bitmap, something like this:

// Create an offscreen buffer
int layer = c.saveLayer(0, 0, width, height, null,
        Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);

// Setup a paint object for the path
mPaintPath.setARGB(255, 255, 255, 255);
mPaintPath.setStyle(Paint.Style.FILL_AND_STROKE);
mPaintPath.setAntiAlias(true);

// Draw the path onto the offscreen buffer
mPath.moveTo(x0, y0);
mPath.lineTo(x1, y1);
mPath.lineTo(x2, y2);
mPath.lineTo(x3, y3);
mPath.lineTo(x0, y0);
mPath.close();
c.drawPath(mPath, mPaintPath);

// Draw a bitmap on the offscreen buffer and use the path that's already
// there as a mask
mBitmapPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
c.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

// Composit the offscreen buffer (a masked bitmap) to the canvas
c.restoreToCount(layer);

如果您可以承受锯齿,则有一种更简单的方法:只需设置一个剪切路径(请注意使用 Region.Op.Difference 会导致路径内部被剪切掉,而不是剪切路径外部的所有内容):

If you can withstand aliasing, there's an easier way: just setup a clip path (note the use of Region.Op.DIFFERENCE which causes the insides of the path to be clipped away rather than clipping everything outside of the path):

// Setup a clip path
mPath.moveTo(x0, y0);
mPath.lineTo(x1, y1);
mPath.lineTo(x2, y2);
mPath.lineTo(x3, y3);
mPath.lineTo(x0, y0);
mPath.close();
c.clipPath(mPath, Op.DIFFERENCE);

// Draw the bitmap using the path clip
c.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

这篇关于PorterDuff和路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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