Draw area between two lines using Canvas on Androi

2019-06-12 07:52发布

问题:

I'm developing a simple statistical graphics class for my application. I've tried aChartEngine and others more, but I prefer use my own classes. I'm drawing the graphics with the Canvas class that includes Android, but the problem is that I don't know how to fill the area between the line and the bottom border. Right now, the rectangles don't fill all the area, obviously, do You know any solution? Thanks a lot.

回答1:

Use a Path, with a Paint which has .setStyle(Paint.Style.FILL); called.

Path fillPath = new Path();
fillPath.moveTo(0, 0); // Your origin point
fillPath.lineTo(x1, y1); // First point
// Repeat above line for all points on your line graph
fillPath.lineTo(xN, yN); // Final point
fillPath.lineTo(xN, 0); // Draw from final point to the axis ++
fillPath.lineTo(0, 0); // Same origin point
yourCanvas.drawPath(fillPath, /* Your paint */);

++ Thanks to @TheCapn for this bit.