简体   繁体   English

当位图适合屏幕并创建缩放位图时,floodfill android无法正常工作

[英]floodfill android not working when the bitmap is fit to screen with create scaled bitmap

In my app i used flood fill for filling the part of the bitmap with color. 在我的应用程序中,我使用泛洪填充将颜色填充到位图的一部分。 Everything worked fine, but I create the bitmap with createscaledbitmap , it does not work. 一切工作正常,但我使用createscaledbitmap创建了位图,但不起作用。 Please suggest me. 请给我建议。

my code is as follows. 我的代码如下。

I had floodfill run in asynch task. 我在紧急任务中执行了洪水填充。

 @Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:

        p1.x = (int) x;
        p1.y = (int) y;
        final int sourceColor = resultBmp.getPixel((int) x, (int) y);
        final int targetColor = paint.getColor();
        new TheTask(resultBmp, p1, sourceColor, targetColor).execute();
        invalidate();
    }
    return true;
}

public void clear() {
    path.reset();
    invalidate();
}

public int getCurrentPaintColor() {
    return paint.getColor();
}

class TheTask extends AsyncTask<Void, Integer, Void> {

    Bitmap bmp;
    Point pt;
    int replacementColor, targetColor;

    public TheTask(Bitmap bm, Point p, int sc, int tc) {
        this.bmp = bm;
        this.pt = p;
        this.replacementColor = tc;
        this.targetColor = sc;
        pd.setMessage("Filling....");
        pd.show();
    }

    @Override
    protected void onPreExecute() {
        pd.show();

    }

    @Override
    protected void onProgressUpdate(Integer... values) {

    }

    @Override
    protected Void doInBackground(Void... params) {
        FloodFill f = new FloodFill();
        f.floodFill(bmp, pt, targetColor, replacementColor);
        return null;
    }

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

my floodfill class 我的补水课

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

my ondraw method is 我的ondraw方法是

  @Override
protected void onDraw(Canvas canvas) {
    this.canvas = canvas;

    int w = mBitmap.getWidth();
    int h = mBitmap.getHeight();

   resultBmp = null;



    int widthofBitMap = screenWidth;
    int heightofBitMap = widthofBitMap * h / w;

    resultBmp = Bitmap.createScaledBitmap(mBitmap, widthofBitMap, heightofBitMap, true);

    canvas.drawBitmap(resultBmp, 0, 0, paint);
  //if i create only bitmap without scaling it works fine.
 // for ex
 //canvas.drawBitmap(mBitmap,0,0,paint); works fine.

}

please suggest me 请建议我

The flood fill method is taking every pixel and changing its color in thread, So instead of increasing the size using create scaled bitmap, I am using separate bitmaps for different screen sizes. 泛洪填充方法会占用每个像素并在线程中更改其颜色,因此,我不是使用创建比例位图来增加大小,而是针对不同的屏幕尺寸使用单独的位图。 Its a cumbersome process but there is no other way according to my research. 根据我的研究,这是一个繁琐的过程,但是没有其他方法。

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

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