title: How to properly use clipRect?
body: <p>I'm writing a custom <code>View</code>, but I can't really figure out how to use <code>clipRect</code> on a <code>Canvas</code>. I need this because I'm calling <code>draw(Canvas)</code> on another object and I'd like to give it my own (clipped) <code>Canvas</code>. My current solution is:</p>
  
  <pre><code>StaticLayout sl = new StaticLayout(text, tp, (int) (rect.right - rect.left), Alignment.ALIGN_NORMAL, 1f, 0f, true);
  
  Bitmap layoutBitmap = Bitmap.createBitmap((int) (rect.right - rect.left), (int) (rect.bottom - rect.top), Config.ARGB_8888);
  Canvas layoutCanvas = new Canvas(layoutBitmap);
  sl.draw(layoutCanvas);
  
  canvas.drawBitmap(layoutBitmap, null, rect, null);
  </code></pre>
  
  <p>However, this feels dirty, creating a new bitmap and a new canvas every time (I'm using this method to draw text in a box, see <a href="https://stackoverflow.com/questions/6071188/draw-a-textview-inside-a-custom-activity/">my previous question</a>).</p>
  
  <p>What I'd like to do is something like this:</p>
  
  <pre><code>StaticLayout sl = new StaticLayout(text, tp, (int) (rect.right - rect.left), Alignment.ALIGN_NORMAL, 1f, 0f, true);
  
  canvas.save();
  canvas.clipRect(rect, Region.Op.REPLACE);
  sl.draw(canvas);
  canvas.restore();
  </code></pre>
  
  <p>That "feels" much better, except that it doesn't work. Am I using <code>clipRect</code> wrong? Do I not understand what it's actually for or how to use it? Please advise.</p>
  
  <p>P.S. My understanding of <code>clipRect</code> is that after that <code>clipRect</code> call, <code>0, 0</code> should actually translate to <code>rect.left, rect.top</code>.</p>