我需要在自定义视图上用给定多边形的区域填充颜色。我找不到解决该问题的方法。
实际上我只能画出连接这些点的线,但是我不确定如何填充它们所包围的区域。
我用来画线的以下代码:
for (Path p : paths) {
Paint color = p.getColor();
float strokeW = color.getStrokeWidth();
color.setStrokeWidth(pthStrokeWdth);
List<Point> currentPath = p.getCoordinateList();
for (int i = 1; i < currentPath.size(); i++) {
float startX = currentPath.get(i - 1).x;
float startY = currentPath.get(i - 1).y;
float endX = currentPath.get(i).x;
float endY = currentPath.get(i).y;
canvas.drawLine((startX * cellWidth) + xOffset + halfCellWidth, (startY * cellHeight) + yOffset + halfCellHeight,
(endX * cellWidth) + xOffset + halfCellWidth, (endY * cellHeight) + yOffset + halfCellHeight, color);
}
color.setStrokeWidth(originalStrokeWidth);
}
上面的绘制线在每个点之间形成了形状多样的多边形。请参见下面的屏幕截图。
所以我想像下面那样填充多边形内部的区域:
感谢您的时间。
答案 0 :(得分:0)
您需要将Paint.Style设置为FILL
或FILL_AND_STROKE
paint.setStyle(Paint.Style.FILL);
编辑:
Paint color = p.getColor();
float strokeW = color.getStrokeWidth();
color.setStrokeWidth(pthStrokeWdth);
color.setStyle(Paint.Style.FILL);
List<Point> currentPath = p.getCoordinateList();
//for each path we want to draw a line which consist of 4 points
float[] points = new float[currentPath.size() * 4];
int count = 0;
for (int i = 1; i < currentPath.size(); i++) {
float startX = currentPath.get(i - 1).x;
float startY = currentPath.get(i - 1).y;
float endX = currentPath.get(i).x;
float endY = currentPath.get(i).y;
points[count++] = (startX * cellWidth) + xOffset + halfCellWidth;
points[count++] = (startY * cellHeight) + yOffset + halfCellHeight;
points[count++] = (endX * cellWidth) + xOffset + halfCellWidth;
points[count++] = (endY * cellHeight) + yOffset + halfCellHeight, color;
}
canvas.drawLines(points, color);