I am not able to find a floodfill algorithm implementation for Android.
Any idea if a floodfill API is available in Android, and if not, is there any other alternative?
Do you have some definition of the shape?
If so, have a look at the Canvas docs. You can fill a region by defining a clip area and then calling canvas.drawColor.
Rough example:
Rect r = new Rect(0,0,300,300);
canvas.clipRect(r); // see also clipRegion
canvas.drawColor(Color.RED);
There are several clip functions, so you should be able build whatever you're trying to fill.
On the other hand, if you want to floodfill a region in a loaded bitmap, then I don't know.
FloodFill in android
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);
}
}
}
You should use a asynctask to use the floodfill algorithm. Using the same on the main thread caused out of memory error. Even if i use floofill algorithm sometime filling a huge area takes more time causing the application to become unresponsive at time.
Fill the complete canvas but keep the bound fill area as it is like circle, rectangle. This link could solve your problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With