简体   繁体   English

Android 中的 Floodfill 算法

[英]Floodfill algorithm in Android

I am not able to find a floodfill algorithm implementation for Android.我找不到适用于 Android 的洪水填充算法实现。

Any idea if a floodfill API is available in Android, and if not, is there any other alternative?不知道 Android 中是否提供了 floodfill API,如果没有,还有其他选择吗?

FloodFill in android安卓中的 FloodFill

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.您应该使用 asynctask 来使用 floodfill 算法。 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.即使我使用 floofill 算法,有时填充一个巨大的区域也需要更多的时间,导致应用程序有时无响应。

Fill the complete canvas but keep the bound fill area as it is like circle, rectangle . 填充整个画布,但保留绑定的填充区域,就像 circle, rectangle 一样 This link could solve your problem此链接可以解决您的问题

Do you have some definition of the shape?你对形状有一些定义吗?

If so, have a look at the Canvas docs .如果是这样,请查看Canvas 文档 You can fill a region by defining a clip area and then calling canvas.drawColor.您可以通过定义一个剪辑区域然后调用 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.另一方面,如果你想在加载的位图中填充一个区域,那么我不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM