Search code examples
javaandroidandroid-studiopathpaint

How do I paint a path? I'm getting a small bug


Image:

Click to see image

I was able to get the other shapes to be painted correctly this same way but for some reason, when I put the second corner of the last shape lower than the shape's forth corner, this bug occurs (the weird rectangle with a weird color).

The coordinates are all fine, but here you go. (w = screen max width, h = screen max height)

paint.setARGB(50, 0, 0, 0);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
path.setFillType(Path.FillType.EVEN_ODD);
path.moveTo(w, h/10);
path.lineTo(w, 5*h/16);
path.lineTo(0, 2*h/5);
path.lineTo(0, h/4);
path.lineTo(w, h/10);

...

canvas.drawPath(path, paint);

Maybe it's about the Path.FillType?

Thanks in advance.


Solution

  • Got it!

    Apparently it's about the number of corners. It should be an odd number. I just added a random new point like this:

    paint.setARGB(50, 0, 0, 0);
    paint.setStyle(Paint.Style.FILL);
    paint.setAntiAlias(true);
    path.setFillType(Path.FillType.EVEN_ODD);
    path.moveTo(w, h/10);
    path.lineTo(w, 5*h/16);
    path.lineTo(0, 2*h/5);
    

    path.lineTo(0, 3*h/5);

    path.lineTo(0, h/4);
    path.lineTo(w, h/10);
    

    Works perfect now!