Rabbit
Rabbit

Reputation: 125

Rounded Rectangle with Android Canvas

I'm attempting to draw a rectangle around a custom view in Android. I mostly have it working except for one detail.

Here is my code...

Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(14.5f);
paint.setStyle(Paint.Style.STROKE);

canvas.drawRoundRect(0, 0, getWidth(), getHeight(), 20.0f, 20.0f, paint);

And here is the resulting rectangle...

enter image description here

As you can see, the inside of the rectangle does have rounded corners, but the outside still draws pointed corners. How can I make it so that the outside corners are rounded as well?

Upvotes: 4

Views: 4788

Answers (1)

Bö macht Blau
Bö macht Blau

Reputation: 13009

You don't see corners rounded on the outside because part of the stroke is outside of the bounds of the Canvas. You can check this easily by adding a certain margin to the coordinates of the round rectangle to make sure it is drawn inside the Canvas.

In fact your best option is trying to optimize this margin depending on the selected stroke width.

Upvotes: 2

Related Questions