user2424998
user2424998

Reputation: 21

canvas.drawArc() using Stroke Paint very imprecise

I'm trying to create a thick (~40dp) partitioned circle using the canvas.drawArc() method and a Paint with STROKE set as style.

But this onDraw() implementation

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
    p.setColor(Color.BLUE);
    p.setStrokeWidth(getResources().getDimension(R.dimen.circle_stroke_width));
    p.setStyle(Paint.Style.STROKE);

    RectF rect = new RectF(
            getWidth()*0.1f,
            getWidth()*0.1f+200,
            getWidth()*0.9f,
            getWidth()*0.9f+200
    );

    canvas.drawArc(rect, 0, 180, false, p);
    canvas.drawArc(rect, 180, 180, false, p);

}

gives me this result

Link to the result picture because I don't have any reputation yet

Is there a way to prevent these gaps when connecting multiple arcs using this method?

Upvotes: 2

Views: 1473

Answers (1)

Loud2004
Loud2004

Reputation: 21

I had a similar issue. In the Android Studio preview window my custom View was drawing fine, but when running it on my HTC One M8 with Android 5.0.2 there was a slight gap at 0 degrees.

I set the view to use software rendering and it fixed the issue:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Hardware Rendering:                                  Software Rendering:

gap no gap

Upvotes: 2

Related Questions