How to draw a filled triangle in an android canvas?

So, I draw this triangle on Android maps using the code below in the drawing method:

paint.setARGB(255, 153, 29, 29); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setAntiAlias(true); Path path = new Path(); path.moveTo(point1_returned.x, point1_returned.y); path.lineTo(point2_returned.x, point2_returned.y); path.moveTo(point2_returned.x, point2_returned.y); path.lineTo(point3_returned.x, point3_returned.y); path.moveTo(point3_returned.x, point3_returned.y); path.lineTo(point1_returned.x, point1_returned.y); path.close(); canvas.drawPath(path, paint); 

Point X_ is back - these are the coordinates that I get from the fields. These are mainly latitudes and longitudes. The result is a good triangle, but the insider is empty, and so I see the map. Is there a way to fill it somehow?

+68
java android google-maps android-canvas
Aug 17 '10 at 9:37
source share
8 answers

You probably need to do something like:

 Paint red = new Paint(); red.setColor(android.graphics.Color.RED); red.setStyle(Paint.Style.FILL); 

And use this color for your path, not for your ARGB. Make sure that the last point of your path ends at the first, it also makes sense.

Please tell me if this works!

+34
Aug. 17 '10 at 9:50
source share

Ok, I did it. I use this code if someone needs it:

 super.draw(canvas, mapView, true); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStrokeWidth(2); paint.setColor(android.graphics.Color.RED); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setAntiAlias(true); Point point1_draw = new Point(); Point point2_draw = new Point(); Point point3_draw = new Point(); mapView.getProjection().toPixels(point1, point1_draw); mapView.getProjection().toPixels(point2, point2_draw); mapView.getProjection().toPixels(point3, point3_draw); Path path = new Path(); path.setFillType(Path.FillType.EVEN_ODD); path.moveTo(point1_draw.x,point1_draw.y); path.lineTo(point2_draw.x,point2_draw.y); path.lineTo(point3_draw.x,point3_draw.y); path.lineTo(point1_draw.x,point1_draw.y); path.close(); canvas.drawPath(path, paint); //canvas.drawLine(point1_draw.x,point1_draw.y,point2_draw.x,point2_draw.y, paint); return true; 

Thanks for the tip of Nicholas!

+64
Aug 17 '10 at
source share

you can also use vertex:

 private static final int verticesColors[] = { Color.LTGRAY, Color.LTGRAY, Color.LTGRAY, 0xFF000000, 0xFF000000, 0xFF000000 }; float verts[] = { point1.x, point1.y, point2.x, point2.y, point3.x, point3.y }; canvas.drawVertices(Canvas.VertexMode.TRIANGLES, verts.length, verts, 0, null, 0, verticesColors, 0, null, 0, 0, new Paint()); 
+10
Aug 18 '10 at 7:51
source share
 private void drawArrows(Point[] point, Canvas canvas, Paint paint) { float [] points = new float[8]; points[0] = point[0].x; points[1] = point[0].y; points[2] = point[1].x; points[3] = point[1].y; points[4] = point[2].x; points[5] = point[2].y; points[6] = point[0].x; points[7] = point[0].y; canvas.drawVertices(VertexMode.TRIANGLES, 8, points, 0, null, 0, null, 0, null, 0, 0, paint); Path path = new Path(); path.moveTo(point[0].x , point[0].y); path.lineTo(point[1].x,point[1].y); path.lineTo(point[2].x,point[2].y); canvas.drawPath(path,paint); } 
+4
May 8 '12 at 11:06
source share

enter image description here

this function shows how to create a triangle from a bitmap. That is, create an image in the form of a triangular shape. Try the code below or a demo download

  public static Bitmap getTriangleBitmap(Bitmap bitmap, int radius) { Bitmap finalBitmap; if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false); else finalBitmap = bitmap; Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(), finalBitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); Paint paint = new Paint(); final Rect rect = new Rect(0, 0, finalBitmap.getWidth(), finalBitmap.getHeight()); Point point1_draw = new Point(75, 0); Point point2_draw = new Point(0, 180); Point point3_draw = new Point(180, 180); Path path = new Path(); path.moveTo(point1_draw.x, point1_draw.y); path.lineTo(point2_draw.x, point2_draw.y); path.lineTo(point3_draw.x, point3_draw.y); path.lineTo(point1_draw.x, point1_draw.y); path.close(); canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.parseColor("#BAB399")); canvas.drawPath(path, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(finalBitmap, rect, rect, paint); return output; } 

The function above returns a triangular image drawn on canvas. More

+4
Jan 01 '16 at 10:52
source share

You need to remove path.moveTo after the first initial one.

 Path path = new Path(); path.moveTo(point1_returned.x, point1_returned.y); path.lineTo(point2_returned.x, point2_returned.y); path.lineTo(point3_returned.x, point3_returned.y); path.lineTo(point1_returned.x, point1_returned.y); path.close(); 
+3
Dec 01 '14 at 15:45
source share

Using @Pavel's answer as a guide, here is a helper method if you don't have points, but have a start x, y and a height and width. You can also draw inverted / inverted - which is useful for me, since it was used as the end of a vertical velvet.

  private void drawTriangle(int x, int y, int width, int height, boolean inverted, Paint paint, Canvas canvas){ Point p1 = new Point(x,y); int pointX = x + width/2; int pointY = inverted? y + height : y - height; Point p2 = new Point(pointX,pointY); Point p3 = new Point(x+width,y); Path path = new Path(); path.setFillType(Path.FillType.EVEN_ODD); path.moveTo(p1.x,p1.y); path.lineTo(p2.x,p2.y); path.lineTo(p3.x,p3.y); path.close(); canvas.drawPath(path, paint); } 
+2
Mar 08 '16 at 17:06
source share

Do not moveTo() after each lineTo()

In other words, delete every moveTo() except the first.

Seriously, if I just copy the OP code and delete the unnecessary calls to moveTo() , it works.

Nothing more to do.




EDIT: I know that the OP has already posted its “final working solution”, but he did not say why it works. The actual reason was quite unexpected for me, so I felt the need to add an answer.

+1
Jul 14 '17 at 12:29
source share



All Articles