title: How to draw line and erase the line I draw on ImageView Android Xamarin
body: <p>I want to let the user to draw lines on the image and user can erase tline they draw. So far I can draw lines on the custom ImageView created as below. However, I tried a lot of method and all of them draw black line when I selected erase mode. I want the part after earse still is the Image I set and implement the erase just like drawing lines. How can I do this?</p>
  
  <pre><code>public class PaintView : ImageView
  {
      private Paint _Paint = new Paint();
      private Paint ErasePaint= new Paint();
      public Bitmap _Bmp;
      private Canvas _Canvas = null;
  
      private PointF _StartPt = new PointF();
      private PointF _EndPt = new PointF();
  
      public int status=1;  //1 draw, 0 erase
  
      public PaintView(Context context, IAttributeSet attrs)
          : base(context, attrs)
      {
  
      }
  
      public void Initialize()
      {
          _Paint = new Paint ();
          _Paint.Color = new Color (255, 255, 255);
          _Paint.StrokeWidth = 5;
          _Paint.StrokeCap = Paint.Cap.Round;
  
          int alpha = 3;
  
          _Bmp = Bitmap.CreateScaledBitmap(_Bmp,_Bmp.Width*alpha, _Bmp.Height*alpha, false);
          _Canvas = new Canvas(_Bmp);
  
          this.SetImageBitmap(_Bmp);
      }
  
  
  
      override public bool OnTouchEvent(MotionEvent e)
      { 
          if (status == 1) {
              switch (e.Action)
              {
              case MotionEventActions.Down: 
                  _StartPt.Set(e.GetX() - 1, e.GetY() - 1);// for just a tapping
                  DrawLine(e);
                  break;
              case MotionEventActions.Move: 
                  DrawLine(e);
                  break;
              case MotionEventActions.Up: 
                  DrawLine(e);
                  break;
              }
          }
          if (status == 0) {
  
              switch (e.Action)
              {
              case MotionEventActions.Down: 
                  _StartPt.Set(e.GetX() - 1, e.GetY() - 1);// for just a tapping
                  eraseCanvas(e);
                  break;
              case MotionEventActions.Move: 
                  eraseCanvas(e);
                  break;
              case MotionEventActions.Up: 
                  eraseCanvas(e);
                  break;
              }
  
          }
  
          return true;
  
      }
  
      private void DrawLine(MotionEvent e)
      {
          _EndPt.Set(e.GetX(), e.GetY());
  
          _Canvas.DrawLine(_StartPt.X, _StartPt.Y, _EndPt.X, _EndPt.Y, _Paint);
  
          _StartPt.Set(_EndPt);
          Invalidate();
      }
  
      private void eraseCanvas(MotionEvent e)
      {
          _EndPt.Set(e.GetX(), e.GetY());
  
          ErasePaint.Color = Color.Transparent;
          ErasePaint.StrokeWidth = 5;
          ErasePaint.SetXfermode (new PorterDuffXfermode (PorterDuff.Mode.Clear));
          _Canvas.DrawLine(_StartPt.X, _StartPt.Y, _EndPt.X, _EndPt.Y, ErasePaint);
  
          _StartPt.Set(_EndPt);
          Invalidate();
      }
  
  }
  </code></pre>