Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw filled polygon?

Tags:

android

How to draw filled polygon in Android ?

like image 627
AndroiDBeginner Avatar asked Jan 12 '10 08:01

AndroiDBeginner


People also ask

How do you draw a filled polygon in Opencv?

Step 1: Import cv2 and numpy. Step 2: Define the endpoints. Step 3: Define the image using zeros. Step 4: Draw the polygon using the fillpoly() function.

How do you draw a closed polygon?

A polygon doesn't have to be regular to be a polygon. If you want to keep your drawing process easy, just use a straight edge and pencil and draw several line segments that interact to form a closed shape. In itself, that's a polygon!

How do you create a filled polygon in Java?

To draw or fill a PolygonUse the Graphics methods g. drawPolygon(p) or g. fillPolygon(p) to draw or fill a polygon, where p is the polygon.


2 Answers

Android does not have a handy drawPolygon(x_array, y_array, numberofpoints) action like Java. You have to walk through making a Path object point by point. For example, to make a filled trapezoid shape for a 3D dungeon wall, you could put all your points in x and y arrays then code as follows:

Paint wallpaint = new Paint(); wallpaint.setColor(Color.GRAY); wallpaint.setStyle(Style.FILL);  Path wallpath = new Path(); wallpath.reset(); // only needed when reusing this path for a new build wallpath.moveTo(x[0], y[0]); // used for first point wallpath.lineTo(x[1], y[1]); wallpath.lineTo(x[2], y[2]); wallpath.lineTo(x[3], y[3]); wallpath.lineTo(x[0], y[0]); // there is a setLastPoint action but i found it not to work as expected  canvas.drawPath(wallpath, wallpaint); 

To add a constant linear gradient for some depth, you could code as follows. Note y[0] is used twice to keep the gradient horizontal:

 wallPaint.reset(); // precaution when resusing Paint object, here shader replaces solid GRAY anyway  wallPaint.setShader(new LinearGradient(x[0], y[0], x[1], y[0], Color.GRAY, Color.DKGRAY,TileMode.CLAMP));    canvas.drawPath(wallpath, wallpaint); 

Refer to Paint, Path and Canvas documentation for more options, such as array defined gradients, adding arcs, and laying a Bitmap over your polygon.

like image 109
Androidcoder Avatar answered Oct 09 '22 11:10

Androidcoder


You need to set the paint object to FILL

Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); 

Then you can draw whatever you want, and it will be filled.

canvas.drawCircle(20, 20, 15, paint); canvas.drawRectangle(60, 20, 15, paint); 

etc.

For more complex shapes you need to use the PATH object.

like image 26
Adrian Fâciu Avatar answered Oct 09 '22 11:10

Adrian Fâciu