I’m creating an application to draw on canvas. To draw I set the pen color to black.
for pen button
int black = Color.BLACK;
mDrawPaint = new DrawPaint(Capture.this, null,black);
where DrawPaint extends View
Now to create eraser I just changed the pen color to white that is the background color of the canvas. Like this
for eraser button
int white = Color.WHITE;
mDrawPaint = new DrawPaint(Capture.this, null,white);
But again if I select the pen button which has pen color black and draw something on canvas, it again automatically redraws the previous paint what I drew before erasing it. Also the eraser erases a big rectangular area. Please explain me what is going wrong. Thank you.
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;
}
For Eraser you can use this code…
and about that square color prob…
Check this out..
this is the best example for canvas drawing with erase, blur and Emboss effect…
}