MobileX
MobileX

Reputation: 419

drawArc strange behavior

The code:

int width = canvas.getWidth();
int height = canvas.getHeight();

int shift = 0;
RectF rect = new RectF(0 + shift, 0 + shift, width - 1 - shift, height - 1 - shift);

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(30);
paint.setColor(0xff009900);

float angle_step = 180 / 5;

for (int i=0; i<5; i++) {
  canvas.drawArc(rect, 180 + angle_step * i, angle_step, true, paint);
}

The result:

The question: How to make inner stroke instead of outer?

Thanks

Upvotes: 2

Views: 728

Answers (1)

j2ko
j2ko

Reputation: 2539

As @Mike M (all credits goes to him) mentioned the solution would be as following:

UPDATE: instead of using Paint.Joint.Round you could use mitter limit as following:

    int width = canvas.getWidth();
    int height = canvas.getHeight();

    int shift = 0;
    RectF rect = new RectF(0 + shift, 0 + shift, width - 1 - shift, height - 1 - shift);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStyle(Paint.Style.STROKE);

    //Make stroke mitter to affect only very sharp angles
    paint.setStrokeMiter(1.8f);

    int strokeWidth = 30;
    paint.setStrokeWidth(strokeWidth);
    paint.setColor(0xff009900);

    //inset drawing rect by the half of stroke width
    rect.inset(strokeWidth/2,strokeWidth/2);

    float angle_step = 180 / 5;

    for (int i=0; i<5; i++) {
        canvas.drawArc(rect, 180 + angle_step * i, angle_step, true, paint);
    }

This will give you sharp adges on left and right.

The result:

enter image description here

Upvotes: 4

Related Questions