27

I need to draw polygons like triangle, trapezoid, pentagon, parallelogram, rhombus etc. It seems Path class is way to go, however i need these polygons have rounded corners and i also need to control the amount of the rounding.

user65721
  • 2,833
  • 3
  • 19
  • 28

1 Answers1

58

Find below a simple example to draw rounded corner polygons (i.e. triangle, rectangle, etc.)

@Override
public void draw(Canvas canvas) {            
    Paint paint = new Paint();
    paint.setColor(Color.GREEN);
    paint.setStrokeWidth(6);
    float radius = 50.0f;
    CornerPathEffect corEffect = new CornerPathEffect(radius);
    paint.setPathEffect(corEffect);
    Path path = new Path();
    path.moveTo(20, 20);
    path.lineTo(400, 20);
    path.lineTo(600, 300);
    path.lineTo(400, 400);
    path.lineTo(20, 400);
    path.close();
    canvas.drawPath(path, paint);
}

In order to control the amount of rounding, change the value of radius.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Nibir
  • 747
  • 6
  • 5