填充整个画布,但保持边界填充区域不变,如圆形、矩形。

5

可能是重复的问题

大家好,我正在创建一个绘图应用程序,遇到了一个问题。如果我画一个没有填充的矩形或者其他类似的边界区域,并改变背景颜色,则矩形填充区域也会改变,这意味着整个画布的颜色都将被新的背景颜色填满。如何保留背景或填充未绑定的画布区域?下面是图片。

这是初始图片:

enter image description here

更改背景颜色后得到以下结果:

enter image description here

但是如何得到以下方式:

enter image description here

3个回答

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

最后使用洪水填充(FloodFill)算法来填充一个封闭区域

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

enter image description here


更改背景后的屏幕截图链接为:http://tinypic.com/view.php?pic=2a9a891&s=6。希望这解决了你的问题。 - Raghunandan
你可以更改背景并使用你想要的颜色填充封闭区域。 - Raghunandan
@TalhaQ,你不能从这里获得答案和代码来解决特定问题。如果你正在寻找代码,很抱歉,这个网站不是提供代码的方式。 - Raghunandan
你说得没错,但我找不到初始化这段代码的MainActivity。 - Talha Q
@TalhaQ,尝试将你的问题发布为新问题,也许你会找到答案。但是不要要求完整的代码,因为你不会得到。你可以在 meta.stackoverflow.com 上讨论该网站的工作方式。 - Raghunandan
显示剩余3条评论

0

0

这里是代码(你必须在触摸事件中绑定形状,否则它会改变形状的颜色):

public class ttt extends View {
MyShape myShape;
public ttt(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    myShape = new MyShape();
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    myShape.setPaint(paint);
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    myShape.onDraw(canvas);     
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    int x = (int) event.getX();
    int y = (int) event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        myShape.setPaint(paint);
        invalidate();
        break;

    default:
        break;
    }

    return super.onTouchEvent(event);
}

class MyShape {
    private Paint paint;
    public MyShape() {
        // TODO Auto-generated constructor stub
    }
    public void onDraw(Canvas canvas){
        canvas.drawCircle(15, 15, 30, getPaint());
    }
    /**
     * @param paint the paint to set
     */
    public void setPaint(Paint paint) {
        this.paint = paint;
    }
    /**
     * @return the paint
     */
    public Paint getPaint() {
        return paint;
    }

}

}


我已经将绘画对象设置为这样,无论我绘制什么形状,该样式都被设置为只有描边而没有填充。如果我同时设置填充和描边,那么两种颜色都是相同的。 - Pratik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接