2012-01-04 70 views
4

可能重複填寫完整的畫布,但保持綁定的填充區域,因爲它是像圓,矩形

朋友你好,

我創建的繪圖應用程序,我有問題。如果我繪製沒有填充的矩形和或其他類似的邊界區域並更改背景顏色,則矩形填充區域也會改變意味着整個畫布的顏色將被填充新的背景顏色。如何讓背景或填補這是不綁定的畫布區域,這裏是圖像

這是最初的圖像

enter image description here

變更後的背景顏色得到這個結果

enter image description here

但如何得到這樣的方式

enter image description here

回答

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

} 

}

+0

我已經像這樣設置了繪畫對象,而且我繪製的任何形狀都被設置爲僅筆畫而不是填充。如果我設置爲填充和描邊,那麼兩種顏色都是相同的 – Pratik 2012-01-04 09:43:18

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

+0

鏈接到屏幕截圖http://tinypic.com/view.php?pic=2udxrtx&s=6 – Raghunandan 2012-10-08 08:24:54

+0

鏈接到屏幕截圖,背景更改http://tinypic.com/ view.php?PIC = 2a9a891&S = 6。希望這能解決你的問題。 – Raghunandan 2012-10-08 08:27:27

+0

您可以更改背景並使用所需顏色填充封閉區域。 – Raghunandan 2012-10-08 08:31:50