填写完整的画布,但保留约束填充区域,因为它是像圆,矩形 [英] Fill the complete canvas but keep the bound fill area as it is like circle, rectangle

查看:127
本文介绍了填写完整的画布,但保留约束填充区域,因为它是像圆,矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复

朋友你好,

我创建的绘图应用程序,我在那有问题。如果我绘制矩形无填充,或其它类似的约束范围,改变背景颜色,然后矩形填充区域也变化意味着整个画布颜色将充满新的背景颜色。如何让背景或填补这是没有约束的画布区域,这里是图像

这是最初的图片

变更后的背景颜色得到这样的结果。

但如何让像这样

解决方案

 最后的点P1 =新的点();
                p1.x =(INT)×; // x坐标,其中用户触摸屏幕上
                p1.y =(INT)Y; // y坐标,其中用户触摸屏幕上
 新TheTask(yourbitmap,P1,sourceColor,targetColor).execute(); //利用asyntask效率

 类TheTask扩展的AsyncTask<太虚,整型,太虚> {

    BMP位图;
    点角;
    INT replacementColor,targetColor;
    ProgressDialog PD;
 公共TheTask(位图BM,点P,INT SC,INT TC)
 {
this.bmp = BM;
this.pt = P;
this.replacementColor = TC;
this.targetColor = SC;
PD =新ProgressDialog(上下文);
pd.setMessage(填充......);
    }
    @覆盖
    在preExecute保护无效(){
            pd.show();

    }

    @覆盖
    保护无效onProgressUpdate(整数...值){

    }

    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
        此时,floodFill F =新的此时,floodFill();
        f.floodFill(BMP,PT,targetColor,replacementColor);
        返回null;
    }

    @覆盖
    保护无效onPostExecute(无效的结果){
pd.dismiss();
无效();
    }
 

最后用此时,floodFill算法来填充禁区

 公共类此时,floodFill {
公共无效此时,floodFill(位图图像​​,点节点,诠释targetColor,
        INT replacementColor){
    INT宽度= image.getWidth();
    INT高= image.getHeight();
    INT目标= targetColor;
    INT置换= replacementColor;
    如果(目标!=更换){
        队列<点>队列=新的LinkedList<点>();
        做 {
            INT X = node.x;
            INT Y = node.y;
            而(X大于0&安培;&安培; image.getPixel(X  -  1,y)的目标==){
                x--;
            }
            布尔spanUp = FALSE;
            布尔spanDown = FALSE;
            而(X LT;宽度安培;&安培; image.getPixel(X,Y)==目标){
                image.setPixel(X,Y,更换);
                如果(spanUp&安培;&安培y与其所连接0安培;&安培;!image.getPixel(X,Y  -  1)==目标){
                    queue.add(新点(X,Y  -  1));
                    spanUp = TRUE;
                }否则如果(spanUp&安培;&安培; Y> 0
                        &功放;&安培; image.getPixel(X,Y  -  1)!=目标){
                    spanUp = FALSE;
                }
                !如果(spanDown&安培;&安培; Y<高度 -  1
                        &功放;&安培; image.getPixel(X,Y + 1)==目标){
                    queue.add(新点(X,Y + 1));
                    spanDown = TRUE;
                }否则如果(spanDown&安培;&安培; Y<高度 -  1
                        &功放;&安培; image.getPixel(X,Y + 1)!=目标){
                    spanDown = FALSE;
                }
                X ++;
            }
        }而((节点= queue.poll())!= NULL);
    }
}
}
 

possible duplicate

Hello friends,

I creating paint application, I have problem in that. If I draw the rectangle without fill and or another like bound area and change the background color then rectangle fill area also change means whole canvas color will be filled with the new background color. How to keep the background or fill the canvas area which was not bound, here is the image

this is the initial image

after change the background color getting this result

but how to getting like this way

解决方案

final Point p1 = new Point();
                p1.x=(int) x; //x co-ordinate where the user touches on the screen
                p1.y=(int) y; //y co-ordinate where the user touches on the screen  
 new TheTask(yourbitmap, p1, sourceColor,targetColor).execute();// use asyntask for efficiency

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

    Bitmap bmp;
    Point pt;
    int replacementColor,targetColor;
    ProgressDialog pd;
 public TheTask(Bitmap bm,Point p, int sc, int tc)
 {
this.bmp=bm;
this.pt=p;
this.replacementColor=tc;
this.targetColor=sc;
pd= new ProgressDialog(context);
pd.setMessage("Filling....");
    }
    @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();
    }

Finally use a FloodFill algorithm to fill a closed area

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

这篇关于填写完整的画布,但保留约束填充区域,因为它是像圆,矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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