2013-02-24 165 views
0

我試圖製作一個Android的孩子們的着色書應用程序,當我第一次繪製一種顏色的確定,但是當我改變筆刷顏色時,所有以前的顏色變化顏色以及。什麼可以解決這個問題?更改筆劃顏色變化以前的筆畫顏色

開始用一種顏色來作畫: enter image description here

改變顏色,使另一個行程,這是什麼之後發生

enter image description here

這裏是我SignatureView類是此圖面:

public class SignatureView extends View { 

    private float STROKE_WIDTH = 5; 

    /** Need to track this so the dirty region can accommodate the stroke. **/ 
    private final float HALF_STROKE_WIDTH = STROKE_WIDTH/2; 

    private Paint paint = new Paint(); 
    private Path path = new Path(); 

    /** 
    * Optimizes painting by invalidating the smallest possible area. 
    */ 
    private float lastTouchX; 
    private float lastTouchY; 
    private final RectF dirtyRect = new RectF(); 

    public SignatureView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     paint.setAntiAlias(true); 
     paint.setColor(Color.CYAN); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeWidth(STROKE_WIDTH); 
    } 

    public float getBrushSize() { 
     return STROKE_WIDTH; 
    } 

    public void setBrushSize(float brushSize) { 
     this.STROKE_WIDTH = brushSize; 
    } 

    public void setColor(int color) { 
     paint.setColor(color); 
    } 

    /** 
    * Erases the signature. 
    */ 
    public void clear() { 
     path.reset(); 

     // Repaints the entire view. 
     invalidate(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.drawPath(path, paint); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     float eventX = event.getX(); 
     float eventY = event.getY(); 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      path.moveTo(eventX, eventY); 
      lastTouchX = eventX; 
      lastTouchY = eventY; 
      // There is no end point yet, so don't waste cycles invalidating. 
      return true; 

     case MotionEvent.ACTION_MOVE: 
     case MotionEvent.ACTION_UP: 
      // Start tracking the dirty region. 
      resetDirtyRect(eventX, eventY); 

      // When the hardware tracks events faster than they are delivered, 
      // the 
      // event will contain a history of those skipped points. 
      int historySize = event.getHistorySize(); 
      for (int i = 0; i < historySize; i++) { 
       float historicalX = event.getHistoricalX(i); 
       float historicalY = event.getHistoricalY(i); 
       expandDirtyRect(historicalX, historicalY); 
       path.lineTo(historicalX, historicalY); 
      } 

      // After replaying history, connect the line to the touch point. 
      path.lineTo(eventX, eventY); 
      break; 

     default: 
      // Log.("Ignored touch event: " + event.toString()); 
      return false; 
     } 

     // Include half the stroke width to avoid clipping. 
     invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH), 
       (int) (dirtyRect.top - HALF_STROKE_WIDTH), 
       (int) (dirtyRect.right + HALF_STROKE_WIDTH), 
       (int) (dirtyRect.bottom + HALF_STROKE_WIDTH)); 

     lastTouchX = eventX; 
     lastTouchY = eventY; 

     return true; 
    } 

    /** 
    * Called when replaying history to ensure the dirty region includes all 
    * points. 
    */ 
    private void expandDirtyRect(float historicalX, float historicalY) { 
     if (historicalX < dirtyRect.left) { 
      dirtyRect.left = historicalX; 
     } else if (historicalX > dirtyRect.right) { 
      dirtyRect.right = historicalX; 
     } 
     if (historicalY < dirtyRect.top) { 
      dirtyRect.top = historicalY; 
     } else if (historicalY > dirtyRect.bottom) { 
      dirtyRect.bottom = historicalY; 
     } 
    } 

    /** 
    * Resets the dirty region when the motion event occurs. 
    */ 
    private void resetDirtyRect(float eventX, float eventY) { 

     // The lastTouchX and lastTouchY were set when the ACTION_DOWN 
     // motion event occurred. 
     dirtyRect.left = Math.min(lastTouchX, eventX); 
     dirtyRect.right = Math.max(lastTouchX, eventX); 
     dirtyRect.top = Math.min(lastTouchY, eventY); 
     dirtyRect.bottom = Math.max(lastTouchY, eventY); 
    } 

    public int getColor() { 
     return paint.getColor(); 
    } 

} 

我改變顏色使用

public void colorpicker() { 
     AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, 
       signature.getColor(), new OnAmbilWarnaListener() { 

        @Override 
        public void onCancel(AmbilWarnaDialog dialog) { 
        } 

        @Override 
        public void onOk(AmbilWarnaDialog dialog, int color) { 
         signature.setColor(color); 
        } 
       }); 
     dialog.show(); 
    } 
+0

你是怎麼解決這個問題的?我想請你幫忙! – VENKI 2013-11-30 00:14:36

+0

@VENKI我解決了這個使用類似的例子,亞歷克斯添加在下面的答案 – 2013-11-30 11:14:31

回答

1

使用新路徑&爲每個筆劃繪製對象。

或者,一旦您擡起手指,將路徑渲染爲位圖並將其用於繪製。

Bitmap drawing; 
final Path path = new Path(); 

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    Bitmap newDrawing = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Config.ARGB_8888); 
    if(drawing != null){ 
     Canvas c = new Canvas(newDrawing); 
     c.drawBitmp(drawing); 
    } 
    drawing = newDrawing; 
} 

public boolean onTouchEvent(MotionEvent e){ 
    int action = e.getAction(); 
    if(action == MotionEvent.ACTION_DOWN){ 
     path.reset(); 
    }else if(MotionEvent.ACTION_MOVE){ 
     if(path.isEmpty()){ 
      path.moveTo(e.getX(), e.getY()); 
     }else{ 
      path.lineTo(e.getX(), e.getY()); 
     } 
    }else if(MotionEvent.ACTION_UP){ 
     drawing.drawPath(path); 
    } 
} 
+0

我怎麼能這樣做..這是好嗎?嘗試過但不工作: paint = new Paint(); \t \t paint.setAntiAlias(true); \t \t paint.setColor(color); \t \t paint.setStyle(Paint.Style.STROKE); \t \t paint.setStrokeJoin(Paint.Join.ROUND); \t \t paint.setStrokeWidth(STROKE_WIDTH); – 2013-02-24 13:27:46

+0

我已經更新了我的答案。 – alex 2013-02-24 13:44:14

+0

你能給我這個類的全部源碼嗎 – 2013-02-24 14:09:33