How to draw a filled polygon?

How to draw a filled polygon in Android?

+46
android
Jan 12 '10 at 8:16
source share
7 answers

You need to set the drawing 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); 

and etc.

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

+40
Jan 12
source share
โ€” -

Android does not have the convenient drawPolygon(x_array, y_array, numberofpoints) action drawPolygon(x_array, y_array, numberofpoints) such as Java. You have to go through the creation of the Path object by points. For example, to create a filled trapezoid shape for a 3D dungeon wall, you can put all of your points into x and y arrays, and then encode 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 a certain depth, you can code as follows. Note that y [0] is used twice to maintain the horizontal gradient:

  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); 

See Paint , Path, and Canvas for more options, such as gradients defined by an array, adding arcs, and setting a bitmap over your polygon.

+91
Jun 04 2018-12-12T00:
source share

I like to do this in three steps ...

Step 1. Create a pointy class; -)

 /** * Simple point */ private class Point { public float x = 0; public float y = 0; public Point(float x, float y) { this.x = x; this.y = y; } } 

Step 2. Add a method / function to draw

 /** * Draw polygon * * @param canvas The canvas to draw on * @param color Integer representing a fill color (see http://developer.android.com/reference/android/graphics/Color.html) * @param points Polygon corner points */ private void drawPoly(Canvas canvas, int color, Point[] points) { // line at minimum... if (points.length < 2) { return; } // paint Paint polyPaint = new Paint(); polyPaint.setColor(color); polyPaint.setStyle(Style.FILL); // path Path polyPath = new Path(); polyPath.moveTo(points[0].x, points[0].y); int i, len; len = points.length; for (i = 0; i < len; i++) { polyPath.lineTo(points[i].x, points[i].y); } polyPath.lineTo(points[0].x, points[0].y); // draw canvas.drawPath(polyPath, polyPaint); } 

Step 3. Drawing

  drawPoly(canvas, 0xFF5555ee, new Point[]{ new Point(10, 10), new Point(15, 10), new Point(15, 20) }); 

Yes, perhaps you could do it more efficiently, but probably not much more readable :-).

+11
Dec 31 '13 at 22:31
source share

BTW - I found that as soon as you start creating your path, any moveTo commands in the path will mean that the form remains blank.

It makes sense when you think about it, that Android / Java will leave the form blank because moveTo will represent a gap in the polygon.

However, I saw some tutorials like this How to draw a filled triangle in an android canvas?

which have moveTo after each row of To. Although this can lead to a continuous polygon, Android suggests that moveTo represents a gap in the polygon.

+2
Jun 25 2018-12-12T00: 00Z
source share

An old question, but a trick for those who find it. If you include a font with the desired polygon as a glyph, you can use the drawText function to draw your polygon.

The disadvantage is that you need to know in advance which forms you will need. On top of this, if you know in advance, you can include a beautiful library of forms. This code assumes that you have a font called shapes in the folder of your resources / fonts of your project.

  TypeFace shapesTypeFace = Typeface.createFromAsset(getAssets(), "fonts/shapes.ttf"); Paint stopSignPaint = new Paint(); stopSignPaint.setColor(Color.RED); //set anti-aliasing so it looks nice stopSignPaint.setAntiAlias(true); stopSignPaint.setTextSize(200); stopSignPaint.setTypeface(shapesTypeFace); //will show up as a box or question mark since //the current display font doesn't have this glyph. //open the shapes font in a tool like Character Map //to copy and paste the glyph into your IDE //saving it as a variable makes it much easier to work with String hexagonGlyph = "๎˜" String triangleGlyph = "๎˜" ....whatever code you got... //arguments: text, x coordinate, y coordinate, paint canvas.drawText(hexagonGlyph, 200, 100, stopSignPaint); //make it into a go sign stopSignPaint.setColor(Color.Green); canvas.drawText(hexagonGlyph, 200, 100, stopSignPaint); //make a tiny one stopSignPaint.setTextSize(20); stopSignPaint.setColor(Color.RED); canvas.drawText(hexagonGlyph, 200, 100, stopSignPaint); //make a triangle canvas.drawText(triangleGlyph, 200, 100, stopSignPaint); 
+1
Apr 16 '15 at 21:08
source share

Draw a polygon with sides x and a custom radius:

 private void drawPolygon(Canvas mCanvas, float x, float y, float radius, float sides, float startAngle, boolean anticlockwise, Paint paint) { if (sides < 3) { return; } float a = ((float) Math.PI *2) / sides * (anticlockwise ? -1 : 1); mCanvas.save(); mCanvas.translate(x, y); mCanvas.rotate(startAngle); Path path = new Path(); path.moveTo(radius, 0); for(int i = 1; i < sides; i++) { path.lineTo(radius * (float) Math.cos(a * i), radius * (float) Math.sin(a * i)); } path.close(); mCanvas.drawPath(path, paint); mCanvas.restore(); } 
+1
Apr 22 '16 at 11:23
source share

Try it, or watch the full demo

  Paint paint = new Paint(); paint.setColor(Color.parseColor("#BAB399")); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
0
Jan 01 '16 at 10:47 on
source share



All Articles