Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounded Rectangle with Android Canvas

Tags:

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?

like image 642
Rabbit Avatar asked Jun 21 '26 23:06

Rabbit


1 Answers

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.

like image 70
Bö macht Blau Avatar answered Jun 24 '26 12:06

Bö macht Blau