Reputation: 103
I have a problem filling a path drawn on a canvas. I read all those previous questions saying
Paint red = new Paint();
red.setColor(Color.RED);
red.setStyle(Paint.Style.FILL_AND_STROKE);
should be the way it works. Then I drew my Path like
Path p = new Path();
p.moveTo(100,100); //point1
p.lineTo(200,200);
p.moveTo(200,200); //point2
p.lineTo(100,200);
p.moveTo(100,200); //point3
p.lineTo(100,100);
p.close();
Which should be a closed path in my opinion. After canvas.drawPath(p, red); the triangle/path is drawn but not filled although style is FILL_AND_STROKE. What am I getting wrong?
Upvotes: 0
Views: 1045
Reputation: 34542
There is no path to fill because your path consists of multiple lines, but no coherent polygon. See moveTo
documentation, which reads:
Set the beginning of the next contour to the point (x,y).
So by calling moveTo
you are only drawing lines.
Only use lineTo()
, which also "moves" to the target position. And you can skip the last lineTo()
going to the point of origin, close()
will do this automatically.
// create a triangle
Path p = new Path();
p.moveTo(100,100); //p1
p.lineTo(200,200); //p2
p.lineTo(100,200); //p3
p.close();
Upvotes: 2