Skip to content Skip to sidebar Skip to footer

Draw Path With Hole ( Android )

Does anyone have a hint for me for the following problem ? I would like to draw a filled path ( canvas ) which has a hole in it. In SVG the path definition is the follow : M 100 1

Solution 1:

You should use Path.setFillType(Path.FillType.EVEN_ODD):

final Path path = new Path();
final Paint paint = new Paint();

paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL_AND_STROKE);

path.moveTo(100, 100);
path.lineTo(200, 100);
path.lineTo(200, 200);
path.lineTo(100, 200);
path.close();

path.moveTo(150, 150);
path.lineTo(180, 150);
path.lineTo(180, 180);
path.lineTo(150, 180);
path.close();

path.setFillType(Path.FillType.EVEN_ODD);
canvas.drawPath(path, paint);

Solution 2:

SVG determines what's inside/outside a path by using the fill-rule. Java also allows a winding rule to be set. With Android paths there is also a fillType which works similarly. Perhaps you have different rules set for the java or SVG code?

Post a Comment for "Draw Path With Hole ( Android )"