title: Implementing an eraser in an Android drawing app - black trail and then transparent
body: <p>I have an drawing app for Android and I am currently trying to add a real eraser to it. Before, I had just used white paint for an eraser, but that won't do anymore since now I allow background colors and images. I do this by having an image view underneath my transparent canvas. </p>
  
  <p>The problem that I am facing is that whenever I enable my eraser, it draws a solid black trail while I have my finger down, but once I release it goes to transparent. See the screen shots below: </p>
  
  <p><strong>This is how it looks while my finger is on the screen - a solid black trail</strong>
  <img src="https://i.stack.imgur.com/kte9r.png" alt="How it looks while my finger is still on the screen"></p>
  
  <p><strong>This is what it looks like once I remove my finger from the screen</strong>
  <img src="https://i.stack.imgur.com/ftQSZ.png" alt="How it looks once I let go"></p>
  
  <p>So, it seems like I am getting close, but I can't find the right combination of settings to avoid the black trail while my finger is touching while erasing. Here are some relevant code snippets: </p>
  
  <p><strong>onDraw</strong>
  </p>
  
  <pre><code>@Override
  protected void onDraw(Canvas canvas) {
      canvas.drawColor(Color.TRANSPARENT);
      canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
      canvas.drawPath(mPath, mPaint);
      canvas.drawPath(mPreviewPath, mPaint);
  }
  </code></pre>
  
  <p><strong>onTouchEvent</strong>
  </p>
  
  <pre><code>@Override
  public boolean onTouchEvent(MotionEvent event) {
      float currentX = event.getX();
      float currentY = event.getY();
  
      switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
              touchStart(currentX, currentY);
              invalidate();
              break;
          case MotionEvent.ACTION_MOVE:
              touchMove(currentX, currentY);
              invalidate();
              break;
          case MotionEvent.ACTION_UP:
              touchUp(currentX, currentY);
              invalidate();
              break;
      }
      return true;
  }
  </code></pre>
  
  <p><strong>Current attempt at eraser settings</strong>
  </p>
  
  <pre><code>public void startEraser() {
      mPaint.setAlpha(0);
      mColor = Color.TRANSPARENT;
      mPaint.setColor(Color.TRANSPARENT);
      mPaint.setStrokeWidth(mBrushSize);
      mPaint.setStyle(Paint.Style.STROKE);
      mPaint.setMaskFilter(null);
      mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
      mPaint.setAntiAlias(true);
  }
  </code></pre>
  
  <p>There are several other posts about erasers, but most of them just say to use <code>PorterDuff.Mode.CLEAR</code>, <code>setMakFilter(null)</code> and that that should work. In my case, it doesn't. No matter what I try, I get the black trail first and then the desired result only after I release. </p>
  
  <p>I can provide more code if necessary. </p>