【问题标题】:Fill the complete canvas but keep the bound fill area as it is like circle, rectangle填充整个画布,但保持绑定填充区域为圆形、矩形
【发布时间】:2012-02-02 03:51:57
【问题描述】:

可能重复

朋友们好,

我正在创建绘画应用程序,但我遇到了问题。如果我绘制没有填充和/或其他类似绑定区域的矩形并更改背景颜色,则矩形填充区域也会更改,这意味着整个画布颜色将被新的背景颜色填充。如何保留背景或填充未绑定的画布区域,这是图像

这是初始图像

改变背景颜色后得到这个结果

但是如何变成这样

【问题讨论】:

    标签: android android-canvas


    【解决方案1】:
    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);
        }
    }
    }
    

    【讨论】:

    • 链接到背景已更改的屏幕截图tinypic.com/view.php?pic=2a9a891&s=6。希望这能解决您的问题。
    • 您可以更改背景并用您想要的颜色填充封闭区域。
    • @TalhaQ 你不能这个答案,代码是为了解决一个特定的问题。如果您正在寻找代码,对不起,这不是本网站的工作方式。
    • 你是对的,但我找不到你初始化此代码的主要活动
    • @TalhaQ 此代码不适合您。它用于解决问题用户发布的特定问题。没有人会给你完整的代码。抱歉,stackoverflow 不是这样工作的
    【解决方案2】:

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

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

    }

    【讨论】:

    • 我已经以这种方式设置了绘画对象,并且我绘制的任何形状都设置为仅描边而不是填充。如果我设置为填充和描边,那么两种颜色都是相同的
    【解决方案3】:

    您可能还想在这两个问题中查看使用剪辑区域:

    根据您想做什么,它们可能有用。

    我找到了你的问题,因为我不知道用谷歌搜索什么,而我想要的答案不是使用 Flood Fill。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 2016-02-24
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 2013-07-22
      相关资源
      最近更新 更多