2013-02-21 99 views
1

我正在繪製應用程序和下面的代碼工作正常,除了當我想改變漆線的顏色,所有以前畫線將改爲新顏色:android繪製應用程序行一旦改變顏色先前繪製的所有線繪製了新顏色

private Bitmap bitmap; // drawing area for display or saving 
    private Canvas bitmapCanvas; // used to draw on bitmap 
    private Paint paintLine; // used to draw lines onto bitmap 
    private Path mPath; 
    private ArrayList<Path> paths = new ArrayList<Path>(); 
    private ArrayList<Path> undonePaths = new ArrayList<Path>(); 
    private float mX, mY; 
    private static final float TOUCH_TOLERANCE = 4;  

    // DoodleView constructor initializes the DoodleView 

    public DoodleView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); // pass context to View's constructor 
     this.context_new=context; 
     setFocusable(true); 
     setFocusableInTouchMode(true);  

     // set the initial display settings for the painted line 
     paintLine = new Paint(); 
     paintLine.setAntiAlias(true); // smooth edges of drawn line 
     paintLine.setDither(true); 
     paintLine.setColor(Color.BLACK); // default color is black 
     paintLine.setStyle(Paint.Style.STROKE); // solid line 
     paintLine.setStrokeJoin(Paint.Join.ROUND); 
     paintLine.setStrokeWidth(5); // set the default line width 
     paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends 

     bitmapCanvas = new Canvas(); 
     mPath = new Path();    
    } // end DoodleView constructor 

    // Method onSizeChanged creates BitMap and Canvas after app displays 

    @Override 
    public void onSizeChanged(int w, int h, int oldW, int oldH) 
    { 
     super.onSizeChanged(w, h, oldW, oldH); 
     DoodlzViewWidth = w;  
     DoodlzViewHeight = h;   
    } 

    @Override 

    protected void onDraw(Canvas canvas) 
    {   
     for (Path p : paths){canvas.drawPath(p, paintLine);} 
     canvas.drawPath(mPath, paintLine); 
     Log.i("OnDRAWING", "REACH ON DRAW");   
    } 

    // START TOUCH: handle touch event 
    @Override 

    public boolean onTouchEvent(MotionEvent event) 
    {    
      float x = event.getX(); 
      float y = event.getY(); 

      switch (event.getAction()) 
      { 
       case MotionEvent.ACTION_DOWN: 
        touch_start(x, y); 
        invalidate(); 
        break; 
       case MotionEvent.ACTION_MOVE: 
       touch_move(x, y); 
        invalidate(); 
        break; 
       case MotionEvent.ACTION_UP: 
        touch_up(); 
        invalidate(); 
        break; 
      } 
      return true; 
    } 

    private void touch_start(float x, float y) 
    { 
     undonePaths.clear(); 
     mPath.reset(); 
     mPath.moveTo(x, y); 
     mX = x; 
     mY = y; 
    } 

    private void touch_move(float x, float y) 
    { 
     float dx = Math.abs(x - mX); 
     float dy = Math.abs(y - mY); 
     if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) 
     { 
      mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
      mX = x; 
      mY = y; 
     } 
    } 

    private void touch_up() 
    { 
     mPath.lineTo(mX, mY);  
     bitmapCanvas.drawPath(mPath, paintLine);// commit the path to our offscreen 
     paths.add(mPath); 
     mPath = new Path(); 
    } 

    public void onClickUndo() 
    { 
     if (paths.size()>0) 
     { 
      undonePaths.add(paths.remove(paths.size()-1)); 
      invalidate(); 
     }  
     else Toast.makeText(getContext(), "nothing more to undo", Toast.LENGTH_SHORT).show(); 
    } 

setDrawingColor:

public void setDrawingColor(int color) // set the painted line's color 
    {  
     paintLine.setColor(color); 
    } 

怎麼可能上面的代碼進行修改,使得前行固定在其原來的顏色???謝謝!!

回答

1

您需要跟蹤路徑顏色並在onDraw中恢復最初用於繪製路徑的顏色。

protected void onDraw(Canvas canvas) 
{   
    for (Path p : paths){canvas.drawPath(p, paintLine);} 
    paintLine.setColor(restoreColorForPath(p)); 
    canvas.drawPath(mPath, paintLine); 
    Log.i("OnDRAWING", "REACH ON DRAW");   
} 

你只需要執行restoreColorForPath,它是康特部分storeColorForPath(path,color)(可能用一個簡單的Map

這類似的問題可以幫助你:How to draw the multiple lines with different color and undo,redo the paths in android?

+0

謝謝很多!有用!現在上面的代碼沒有背景,只是想知道是否有背景位圖(用戶可以在圖片中加載),我剛剛嘗試添加canvas.drawBitmap(bitmap,0,0,null);在onDraw中,那麼undo函數根本就不會執行。它應該是覆蓋路徑的位圖。如果那麼如何進一步修改呢? – pearmak 2013-02-21 13:56:01

0
protected void onDraw(Canvas canvas) 
{   
    for (Path p : paths){canvas.drawPath(p, paintLine);} 
    canvas.drawPath(mPath, paintLine); 
    Log.i("OnDRAWING", "REACH ON DRAW");   
} 

試試這個代碼,而不是上方

protected void onDraw(Canvas canvas) 
{   
    canvas.drawBitmap(bitmapCanvas, 0,0,paintLine); 
    canvas.drawPath(mPath, paintLine); 
    Log.i("OnDRAWING", "REACH ON DRAW");   
} 
+0

如何修改我的代碼來解決問題? http://stackoverflow.com/questions/18521661/change-color-without-affecting-anything-previously-drawn – Si8 2013-08-30 16:35:41

+0

當您繪製線時,您正在繪製linemapCanvas上的線,該線具有用不同顏色繪製的不同路徑,但你沒有在onDraw中使用它,確定你再次使用路徑 – 2013-08-31 06:47:28

+0

,但在這種情況下,撤消功能可能不起作用 – 2013-08-31 06:48:32