洪水填充在Android着色 [英] flood fill coloring on android

查看:221
本文介绍了洪水填充在Android着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是尝试使用此时,floodFill类,我发现奇怪的问题与着色。

i just try to use FloodFill class and i found strange problem with coloring.

让我们从code启动:

Lets start with code:

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);
    }
}
}

在这里我使用此时,floodFill

和方法:

And method where i use FloodFill:

public void colorize()
    {
        bmp = ((BitmapDrawable)view.getDrawable()).getBitmap();

        view.setOnTouchListener(new ImageView.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                int x = (int)event.getX();
                int y = (int)event.getY();

                ...

                flood.floodFill(bmp, new Point(x, y), bmp.getPixel(x, y), color);
                view.setImageBitmap(bmp);

                ...
            }
        });
    }

如果我尝试使用标准的Andr​​oid色F.G:Color.RED和Color.GREEN,一切工作正常。我可以取代它的绿化工程F.G红,但如果我尝试使用自定义的颜色像这样:F.G. Color.rgb(34,198,67)我得到单点彩色的,而不是填充形状。

If i try to use standard android color f.g: Color.RED and Color.GREEN, everything works fine. I can replace f.g red with green its works, but if i try to use custom color like this: f.g. Color.rgb(34, 198, 67) i get single point colored instead of filled shape.

您能不能帮我找到这个问题的解决方案?

Can You help me to find a solution for this problem?

EDIT1:

我spoted一些有趣的事情。自定义颜色似乎是有些不同的像素值,但我不知道为什么,如果我用洪水填充。

I spoted something interesting. Custom colors seems to be different values on some pixels but i dont know why if i using flood-fill.

推荐答案

问题解决了。位图我曾经此时,floodFill是RGB_565。我只是将其转换为ARGB_8888,一切工作正常。

Problem solved. Bitmap where i used floodfill was RGB_565. I just convert it to ARGB_8888 and everything work fine.

这篇关于洪水填充在Android着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆