Dibujar y borrar la línea en android

Cómo dibujar una línea (no una recta un solo punto continuo aleatorio (como dibujamos con el lápiz)) y después para borrarlo.

es la aplicación de pintura de dedo

////////////******************* Pinting view *******************/////////////////// public class MyView extends View { int bh = originalBitmap.getHeight(); int bw = originalBitmap.getWidth(); public MyView (Context c) { super(c); //mBitmap = Bitmap.createBitmap(bw,bh,Bitmap.Config.ARGB_8888); mBitmap = Bitmap.createScaledBitmap(originalBitmap,bw,bh,true); mCanvas = new Canvas(mBitmap); mPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG); mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)) ; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); /*mBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap);*/ } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.TRANSPARENT); canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); canvas.drawPath(mPath, mPaint); } ////////************touching evants for painting**************/////// private float mX, mY; private static final float TOUCH_TOLERANCE = 4; private void touch_start(float x, float y) { 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); // commit the path to our offscreen mCanvas.drawPath(mPath, mPaint); // kill this so we don't double draw mPath.reset(); } @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: //if(mode == DRAW) { mView.setOnTouchListener((OnTouchListener) this); } touch_move(x, y); invalidate(); break; case MotionEvent.ACTION_UP: touch_up(); invalidate(); break; } return true; } //end of touch events for image }// end MyView 

para borrar

 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 

Lo que hice fue crear una matriz dinámica con puntos centrales de la línea de lápiz. Si desea que el lápiz sea tan nítido, debe registrar todos los puntos de la matriz. Y dibujó todos los puntos con 1pixel círculos de radio. El radio se puede cambiar como la nitidez del lápiz.

Cuando necesite borrar esto, cuando esté en modo de borrado cuando su dedo o puntero pasen por un punto ya ingresado en la matriz, simplemente lo borra.

clase pública MyView extends View {

  private static final float MINP = 0.25f; private static final float MAXP = 0.75f; private Bitmap mBitmap; private Canvas mCanvas; private Path mPath; private Paint mBitmapPaint; public MyView(Context c) { super(c); mPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap); } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(0xFFAAAAAA); canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); canvas.drawPath(mPath, mPaint); } private float mX, mY; private static final float TOUCH_TOLERANCE = 4; private void touch_start(float x, float y) { 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); // commit the path to our offscreen mCanvas.drawPath(mPath, mPaint); // kill this so we don't double draw mPath.reset(); } @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; } } 

No puedo comentar sobre esa respuesta. para borrar

 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 

puedes dibujar de nuevo por

 mPaint.setXfermode(null); 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.