How to draw a filled triangle in android canvas? How to draw a filled triangle in android canvas? android android

How to draw a filled triangle in android canvas?


Ok I've done it. I'm sharing this code in case someone else will need 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 hint Nicolas!


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, instead of your ARGB. Make sure the last point of your path ends on the first one, it makes sense also.

Tell me if it works please !


you can also use vertice :

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