Search code examples
javaandroidgoogle-mapsandroid-canvas

How to draw a filled triangle in android canvas?


So I'm drawing this triangle in android maps using the code below in my draw 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);

The pointX_returned are the coordinates which I'm getting from the fields. They are basically latitudes and longitudes. The result is a nice triangle but the insider is empty and therefore I can see the map. Is there a way to fill it up somehow?


Solution

  • 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 !