title: How to mask out a simple region when painting in Android?
body: <p>Here is a simplified description: Imagine that I'm given a View class that draws a picture of a wall and I want to draw it with a window cut out. Assume that I extend that View class and override its dispatchDraw() method to do the following. First draw the background if any to be seen through the window. Next I want to mask out the rectangular window region somehow and then call super.dispatchDraw(). Finally I want to remove the mask and draw a person standing at the window so that they are painted over both the background and the wall. How can I do this?</p>
  
  <p>Here is some code that seems close to what I need:</p>
  
  <pre><code>@Override
  protected void dispatchDraw(Canvas into) {
      int w = into.getWidth();
      int h = into.getHeight();
      int w3 = w / 3;
      int h3 = h / 3;
      into.drawLine(0, 0, w, h, mPaint);
      Region protect = new Region(w / 3, h / 3, 2 * w / 3, 2 * h / 3);
      into.clipRegion(protect, Op.INTERSECT);
      into.drawColor(Color.MAGENTA); // or super.dispatchDraw() here.
  }
  </code></pre>
  
  <p>That gives me this:</p>
  
  <p><img src="https://i.stack.imgur.com/KenuS.png" alt="enter image description here"></p>
  
  <p>Which is sort of the opposite of what I want. Notice the Region named "protect" in the code above. I want the magenta fill to happen everywhere <em>except</em> in that region. Specifically, what I want to see is this:</p>
  
  <p><img src="https://i.stack.imgur.com/tiCP9.png" alt="enter image description here"></p>
  
  <p>In the analogy of the window, I should then be in a position to remove the restriction and draw a person or something in the normal way that overlaps both the window and the wall.</p>
  
  <p>EDIT: Here is a simplified working version of the answer by Rajesh CP. I also added a red "foreground" stripe drawn over everything at the end to show that I can remove the restriction as well as add them. Thanks Rajesh!</p>
  
  <pre><code>public class MaskTest extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(new ClippView(getApplicationContext()));
      }
  
      private class ClippView extends View {
          private Paint line_paint = new Paint();
          private Paint strip_paint = new Paint();
  
          public ClippView(Context context) {
              super(context);
              line_paint.setColor(Color.GRAY);
              line_paint.setStrokeWidth(20);
              strip_paint.setColor(Color.RED);
          }
  
          @Override
          protected void onDraw(Canvas canvas) {
              super.onDraw(canvas);
              int w = canvas.getWidth();
              int h = canvas.getHeight();
              int w3 = w / 3;
              int h3 = h / 3;
              // This line represents some unknown background that we are drawing over.
              canvas.drawLine(0, 0, w, h, line_paint);
              // 'protect' represents some area to not paint over until desired.
              Region protect = new Region(w3, h3, 2 * w / 3, 2 * h / 3);
              canvas.clipRegion(protect, Op.DIFFERENCE);
              // Paint magenta over the entire canvas, avoiding the protected region.
              canvas.drawColor(Color.MAGENTA);
              // Remove the protected region.
              Region all = new Region(0, 0, w, h);
              canvas.clipRegion(all, Op.UNION);
              // Draw a wide foreground strip over the entire canvas.
              canvas.drawRect(w / 2 - w / 20, 0, w / 2 + w / 20, h, strip_paint);
          }
      }
  }
  </code></pre>