开发者

Implementing a bucket-like method in Android

I created an application for drawing in Android. I want to have a tool similar to Microsoft Paint's bucket tool - the tool will fill the closed area with the chosen color. So on click, I wrote:

开发者_运维问答
boolean[][] didVisit = new boolean[getWidth()][getHeight()];
                visit ((int)event.getX(), (int)event.getY(), didVisit, mBitmap.getPixel((int)event.getX(), (int)event.getY()));

And the visit method is:

private void visit (int x, int y, boolean[][] didVisit, int color)
        {
            if ( x < 0 || x >= didVisit.length || y < 0 || y >= didVisit[0].length  )
            {
                return;
            }
            if (  mBitmap.getPixel(x,y) != color )
                return;
            if ( didVisit[x][y] )
                return;
            didVisit[x][y] = true;
            mBitmap.setPixel(x, y, p.getColor());
            visit(x-1,y,didVisit,color);
            visit(x,y+1,didVisit,color);
            visit(x+1,y,didVisit,color);
            visit(x,y-1,didVisit,color);
        }

However, this causes the application to crush (force close). I'm pretty much perplexed. I reviewed it dozens of times, and still could not see what caused the force close.

I would be glad for any kind of help. Thanks in advance.


I think your app crashes because of stack overflow. Even for 30x30 image this algorithm makes a recursive call with depth 900! I'm not sure how big Android's stack size is, but I'm pretty sure that you should redesign your algorithm to avoid recursion.

UPDATE: According to your comment all I wrote is true. This algorithm will not work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜