在Android应用程序中填充洪水

时间:2014-05-16 06:26:03

标签: android canvas bitmap antialiasing

我正在使用canvas创建绘图应用程序。我使用下面的代码来填充填充,但是在低或中等dpi设备上,图像的某些部分保持未填充,即在不均匀形状的边缘。有些点在非均匀边缘处仍未填充。

    public class FloodFill extends AsyncTask<Void, Void, Void>
    {

        private int width;
        private int height;
        private int target;
        private int replacement;
        private Point node;
        private ProgressDialog progress;

        public FloodFill(Bitmap image, Point node2, int targetColor, int replacementColor)
        {
            flood = Bitmap.createBitmap(image);
            image.recycle();
            this.width = flood.getWidth();
            this.height = flood.getHeight();
            this.target = targetColor;
            this.replacement = replacementColor;
            this.node = node2;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            progress = new ProgressDialog(MainActivity.this);
            progress.setMessage("Filling");
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progress.setIndeterminate(true);
            progress.setCancelable(false);
            progress.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            if (target != replacement)
            {
                Queue<Point> queue = new LinkedList<Point>();
                do
                {
                    int x = node.x;
                    int y = node.y;
                    while (x > 0 && flood.getPixel(x - 1, y) == target)
                    {
                        x--;
                    }                        
                    boolean spanUp = false;
                    boolean spanDown = false;
                    while (x < width && flood.getPixel(x, y) == target)
                    {
                        flood.setPixel(x, y, replacement);
                        if (!spanUp && y > 0 && flood.getPixel(x, y - 1) == target)
                        {
                            queue.add(new Point(x, y - 1));
                            spanUp = true;
                        }
                        else if (spanUp && y > 0 && flood.getPixel(x, y - 1) != target)
                        {
                            spanUp = false;
                        }
                        if (!spanDown && y < height - 1 && flood.getPixel(x, y + 1) == target)
                        {
                            queue.add(new Point(x, y + 1));
                            spanDown = true;
                        }
                        else if (spanDown && y < height - 1 && flood.getPixel(x, y + 1) != target)
                        {
                            spanDown = false;
                        }
                        x++;
                    }
                } while ((node = queue.poll()) != null);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            progress.dismiss();
            invalidate();
        }
    }

我需要这方面的帮助,我是否应该尝试实现另一种算法,如果是,那么我应该尝试在洪水填充后对图像实施抗锯齿。

提前致谢。

0 个答案:

没有答案