我已经阅读了20多个问题/答案,但仍然无法得到我想要的。 我想在矩形内切一个圆,如下所示:
这是我的代码:
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setARGB(180, 0, 0, 0);
canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
Path circularPath = new Path();
circularPath.addCircle(getWidth() / 2, getHeight() / 2, radius, Path.Direction.CCW);
canvas.clipPath(circularPath, Region.Op.REPLACE);
canvas.drawColor(0x00000000);
}
我的背景(setARGB
)显示正确,但没有被裁剪。我尝试了不同的Op
值,而不是REPLACE
,强制软件光栅化(因为我已经在某些Android版本上阅读到clipPath
不支持一些Op
)通过在构造函数中调用setLayerType(LAYER_TYPE_SOFTWARE, null);
,但没有效果。我该如何实现所需的效果?
注意:我的最小SDK版本是15,因此我不需要支持低于4.0的任何内容。