I am creating a painting application on canvas. For drawing, I set the pen color to black.
for pen button
int black = Color.BLACK; mDrawPaint = new DrawPaint(Capture.this, null,black);
where drawpaint extends the view
Now, to create an eraser, I just changed the pen color to white, which is the background color of the canvas. Like this
for eraser button
int white = Color.WHITE; mDrawPaint = new DrawPaint(Capture.this, null,white);
But then again, if I select a pen button that has a black pen color and draws something on the canvas, it again automatically redraws the previous paint that I painted before erasing it. Also, the eraser erases a large rectangular area. Please explain to me what is going wrong. Thanks.
Here is the DrawPaint constructor
public DrawPaint(Context context, AttributeSet attrs, int color) { super(context, attrs); this.destroyDrawingCache(); paint.setAntiAlias(true); paint.setColor(color); paint.setStyle(Paint.Style.STROKE); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeWidth(STROKE_WIDTH); } public boolean onTouchEvent(MotionEvent event) { eventX = event.getX(); eventY = event.getY(); button1.setEnabled(true); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: path.moveTo(eventX, eventY); lastTouchX = eventX; lastTouchY = eventY; return true; case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: resetDirtyRect(eventX, eventY); 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); } path.lineTo(eventX, eventY); break; default: debug("Ignored touch event: " + event.toString()); return false; } 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; }