Circular sector shaped clipping mask with Path.addArc?

hthms picture hthms · May 13, 2012 · Viewed 8.1k times · Source

I need to create a clipping mask with a shape of a circular sector.

I am able to draw one using the following:

paint.setColor(0x88FF0000);
paint.setStyle(Style.FILL);
canvas.drawArc(oval, 0, 30, true, paint);

I want to use it as a clipping path, so I've tried:

Path path = new Path();
path.addArc(oval, 0, 30);
canvas.clipPath(path, Op.REPLACE);

However addArc doesn't have the useCenter parameter so what I get is not a sector but a segment.

Answer

hthms picture hthms · May 20, 2012

Ok, it seems there is no proper way doing this using a clipping mask.

However there is an alternate way using PorterDuffXfermode. See Xfermodes in ApiDemos.

I simply draw a sector using drawArc over my image with the DST_OUT operator. This makes the part of the image covered by the sector invisible (not drawn).

paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(0xFFFFFFFF);
paint.setStyle(Style.FILL);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
drawable.draw(canvas);
canvas.drawArc(oval, 30, 90, true, paint);