我正在尝试把画布绘制操作裁剪成弧形楔形。但是,设置画布的裁剪路径后,我没有得到预期的结果。
为了说明,这是我的操作:
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
本身。有什么想法吗?
编辑 作为更新,我找到了一种更有效的方法来实现这个目的,它可以进行硬件加速。首先,将整个图像(您要剪裁的图像)绘制到离屏位图中。使用此Bitmap
创建一个BitmapShader
,将该着色器设置为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
);
}