I draw an arc on canvas in a custom view as shown below. Paint
and rectangle
are defined outside of onDraw()
and added in there for simpli
I've found the solution! :)
Instead of this:
RectF rectangle = new RectF(60f, 60f, 480f, 480f);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(0x40000000);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(120);
canvas.drawArc(rectangle, 225f, 315f, false, paint);
Use this:
RectF rectangle = new RectF(60f, 60f, 480f, 480f);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(0x40000000);
paint.setStyle(Paint.Style.FILL);
float strokeWidth = 120;
float startAngle = 225f;
float sweepAngle = 315f;
RectF pathRect = new RectF();
Path path = new Path();
float s = strokeWidth / 2;
pathRect.left = rectangle.left - s;
pathRect.right = rectangle.right + s;
pathRect.top = rectangle.top - s;
pathRect.bottom = rectangle.bottom + s;
path.arcTo(pathRect, startAngle, sweepAngle);
pathRect.left = rectangle.left + s;
pathRect.right = rectangle.right - s;
pathRect.top = rectangle.top + s;
pathRect.bottom = rectangle.bottom - s;
path.arcTo(pathRect, startAngle + sweepAngle, -sweepAngle);
path.close();
canvas.drawPath(path, paint);
This code results in slower run, but it doesn't produce artefacts. :)