Round sectional edging mask with Path.addArc?

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

I can do 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 tried:

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

However, addArc does not have a useCenter parameter, so I get not a sector, but a segment.

+6
source share
2 answers

Well, it seems that there is no suitable way to do this using a clipping mask.

However, there is an alternative way to use PorterDuffXfermode . See Xfermodes in ApiDemos.

I just draw a sector using drawArc in my image using 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); 
+12
source

I needed to set up the AChartEngine library for Android in order to turn a pie chart into a donut chart. I needed to replace the following:

 canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint); 

using the Path class to be able to use cropping. The following code did the job for me:

 Path path = new Path(); path.moveTo(oval.centerX(), oval.centerY()); path.addArc(oval, startAngle, sweepAngle); path.lineTo(oval.centerX(), oval.centerY()); canvas.drawPath(path, paint); 

Hope this saves some headaches from people not familiar with the Canvas API like me.

+2
source

Source: https://habr.com/ru/post/915602/


All Articles