我找不到适用于Android的泛洪填充算法实现。
请问是否有适用于Android的泛洪填充API,如果没有,是否有其他替代方案?
您是否对形状有一些定义?
如果是这样,请查看Canvas文档。您可以通过定义剪辑区域并调用canvas.drawColor来填充区域。
粗略示例:
Rect r = new Rect(0,0,300,300);
canvas.clipRect(r); // see also clipRegion
canvas.drawColor(Color.RED);
有几个剪辑函数,所以你应该能够构建任何你想要填充的内容。
另一方面,如果你想要在加载的位图中填充一个区域,那我就不知道了。
public class FloodFill {
public void floodFill(Bitmap image, Point node, int targetColor,
int replacementColor) {
int width = image.getWidth();
int height = image.getHeight();
int target = targetColor;
int replacement = replacementColor;
if (target != replacement) {
Queue<Point> queue = new LinkedList<Point>();
do {
int x = node.x;
int y = node.y;
while (x > 0 && image.getPixel(x - 1, y) == target) {
x--;
}
boolean spanUp = false;
boolean spanDown = false;
while (x < width && image.getPixel(x, y) == target) {
image.setPixel(x, y, replacement);
if (!spanUp && y > 0 && image.getPixel(x, y - 1) == target) {
queue.add(new Point(x, y - 1));
spanUp = true;
} else if (spanUp && y > 0
&& image.getPixel(x, y - 1) != target) {
spanUp = false;
}
if (!spanDown && y < height - 1
&& image.getPixel(x, y + 1) == target) {
queue.add(new Point(x, y + 1));
spanDown = true;
} else if (spanDown && y < height - 1
&& image.getPixel(x, y + 1) != target) {
spanDown = false;
}
x++;
}
} while ((node = queue.poll()) != null);
}
}
}