I've read over 20 questions/answers but I still can't get what I want. I want to cut a circle inside a rectangle as seen below:
Here is my code:
@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);
}
My background (setARGB
) displays correctly, however nothing is clipped. I've also tried different Op
values other than REPLACE
, forced software rasterization (as I've read on some Android versions clipPath
doesn't support some of the Op
s) by calling setLayerType(LAYER_TYPE_SOFTWARE, null);
on constructor, but no avail. How do I achieve the desired effect?
Note: My minimum SDK version is 15, so I don't need to support anything lower than 4.0.