title: Clear paint from canvas without clearing background image - Android
body: <p>I have a canvas with a background image. I need to know if it's possible to clear the paint from this canvas for redraw without clearing its background image. Here is my example and my results so far.</p>
  
  <p><strong>JAVA</strong></p>
  
  <pre><code>public void setCanvas() {
      if(mFile != null &amp;&amp; mFile.exists()) {
          mPictureBitmap = BitmapFactory.decodeFile(mFile.getAbsolutePath());
          mBitmap = Bitmap.createScaledBitmap(mPictureBitmap, mImageView.getWidth(), mImageView.getHeight(), false);
          mBitmap = mBitmap.copy(Bitmap.Config.ARGB_8888, true);
          mCanvas = new Canvas(mBitmap);
  
          mImageView.setImageBitmap(mBitmap);
          mImageView.setOnTouchListener(this);
      }
  }
  
  private void draw() {
      mCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR); // THIS LINE CLEARS
  
      int lastIndex = mCordHashMap.size() - 1;
      int index = 0;
  
      for (LinkedHashMap.Entry&lt;String, ArrayList&lt;Circle&gt;&gt; entry : mCordHashMap.entrySet()) {
          ArrayList&lt;Circle&gt; coords = new ArrayList&lt;Circle&gt;();
          coords = entry.getValue();
          Path path = new Path();
          String key = entry.getKey();
          String surface = getSurfaceFromKey(key);
          changePaint(surface);
  
          if (coords.size() &lt; 3) {
              for (Circle c : coords) {
                  mCanvas.drawCircle(c.getmX(), c.getmY(), RADIUS, mCirclePaint);
              }
          } else {
              for (Circle c : coords) {
                  if (c == coords.get(0)) {
                      path.moveTo(c.getmX(), c.getmY());
                  } else {
                      path.lineTo(c.getmX(), c.getmY());
                  }
              }
  
              path.close();
              mCanvas.drawPath(path, mPaint);
              mCanvas.drawPath(path, mStrokePaint);
  
          }
  
          if (index == lastIndex &amp;&amp; !mSpecificPathSelected) {
              for (Circle c : coords) {
                  mCanvas.drawCircle(c.getmX(), c.getmY(), RADIUS, mCirclePaint);
                  mCanvas.drawCircle(c.getmX(), c.getmY(), RADIUS, mCircleStrokePaint);
              }
          }
          mImageView.invalidate();
          index++;
      }
  }
  </code></pre>
  
  <p><strong>XML</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" &gt;
  
      &lt;ExpandableListView
          android:id="@+id/surfaceList"
          android:background="@color/light_grey"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight=".20" /&gt;
  
      &lt;ImageView
          android:id="@+id/drawContainer"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight=".80" 
          android:contentDescription="@string/app_name"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>So the code above loads an image into my <code>Canvas</code>. Then each time I touch the <code>Canvas</code> it draws a circle and eventually a path between the circles. However when I start adding more circles and paths, it's redrawing over itself, which looks terrible.</p>
  
  <p>Now I can clear the paint from my canvas with this line.</p>
  
  <pre><code>mCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
  </code></pre>
  
  <p>However I also lose the <code>Bitmap</code> I set as it's background, and now my background is black. So, how do I keep my background image but clear my paint? Is this possible or do I have to use a work around such as two <code>Canvas</code>'s on top of each other? </p>
  
  <p>I have already tried the following, and several variations of the same.</p>
  
  <pre><code>1) mCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
  
  2) mCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
     mCanvas.drawBitmap(mBitmap, 0, 0, mPaint);
  
  3) mBitmap.eraseColor(Color.TRANSPARENT);
     mCanvas.drawBitmap(mBitmap, 0, 0, mPaint);
  </code></pre>
  
  <p>Any help is appreciated</p>