Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw rounded corner polygons on android canvas?

Tags:

I need to draw polygons like triangle, trapezoid, pentagon, parallelogram, rhombus etc. It seems Path class is way to go, however i need these polygons have rounded corners and i also need to control the amount of the rounding.

like image 380
user65721 Avatar asked Nov 11 '14 13:11

user65721


People also ask

How to draw rounded corners in Android canvas?

In Android, we can easily draw objects using the Canvas function. In the case of a complex object, we can create a path and load this path in the canvas for creating the object. To add customized effects, such as rounded corners, we can add path effects inside the canvas.

How to draw a rounded rectangle in AutoCAD?

If you want a rectangle with rounded corners, use drawRoundedRect. It’s similar to drawRect but with the addition of radiusX and radiusY to define the curvature of the rounded corners. It draws an evenly rounded corner if radiusX equals radiusY.

Can a regular polygon be drawn inside the unit circle?

A regular polygon, such as a pentagon, can be drawn inside the unit circle as follows: In order to draw the pentagon we need to be able to identify the 5 points on the unit circle and rotate and draw lines between them. This is where some understanding of trigonometry comes in useful. Let's consider some point, (a, b) on the unit circle as follows:

How to draw a rectangle with rounded corners in Python?

If you want a rectangle with rounded corners, use drawRoundedRect. It’s similar to drawRect but with the addition of radiusX and radiusY to define the curvature of the rounded corners. It draws an evenly rounded corner if radiusX equals radiusY. When radiusX = radiusY, it will have the usual rounded corners as below:


1 Answers

Find below a simple example to draw rounded corner polygons (i.e. triangle, rectangle, etc.)

@Override public void draw(Canvas canvas) {                 Paint paint = new Paint();     paint.setColor(Color.GREEN);     paint.setStrokeWidth(6);     float radius = 50.0f;     CornerPathEffect corEffect = new CornerPathEffect(radius);     paint.setPathEffect(corEffect);     Path path = new Path();     path.moveTo(20, 20);     path.lineTo(400, 20);     path.lineTo(600, 300);     path.lineTo(400, 400);     path.lineTo(20, 400);     path.close();     canvas.drawPath(path, paint); } 

In order to control the amount of rounding, change the value of radius.

like image 58
Nibir Avatar answered Sep 20 '22 19:09

Nibir