我正在尝试将画布绘图操作剪辑到弧形楔形。但是,在将剪切路径设置为画布后,我没有得到预期的结果。
为了说明,这就是我正在做的事情:
path.reset();
//Move to point #1
path.moveTo(rect.centerX(), rect.centerY());
//Per the documentation, this will draw a connecting line from the current
//position to the starting position of the arc (at 0 degrees), add the arc
//and my current position now lies at #2.
path.arcTo(rect, 0, -30);
//This should then close the path, finishing back at the center point (#3)
path.close();
这行得通,当我简单地绘制这条路径 ( canvas.drawPath(path, paint)
) 时,它会绘制如上所示的楔形。但是,当我将此路径设置为画布的剪切路径并在其中绘制时:
//I've tried it with and without the Region.Op parameter
canvas.clipPath(path, Region.Op.REPLACE);
canvas.drawColor(Color.BLUE);
我得到了以下结果(留下楔形只是为了显示参考):
所以它似乎剪裁到 的边界矩形Path
,而不是它Path
本身。有什么想法吗?
编辑就像更新一样,我发现了一种更有效的方法来实现硬件加速。首先,将整个图像(您将要剪裁的)绘制到屏幕外位图中。BitmapShader
使用 this制作 a Bitmap
,将该着色器设置为 a ,然后使用该对象Paint
绘制楔形路径:Paint
drawMyBitmap(bitmap);
Shader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setShader(shader);
@Override
public void onDraw(Canvas canvas) {
canvas.drawArc(rect, //The rectangle bounding the circle
startAngle, //The angle (CW from 3 o'clock) to start
sweepAngle, //The angle (CW from 3 o'clock) of the arc
true, //Boolean of whether to draw a filled arc (wedge)
paint //The paint with the shader attached
);
}