title: Freehand Image Crop draw inside bitmap region
body: <p>Trying to achieve freehand cropping of an image, so I'm able to draw on the image. But it goes outside bitmap region. I just wanna restrict that user can only draw inside bitmap region, check below screen shot.</p>
  
  <p>I am trying to implement functionality like <a href="http://photoshop.wikia.com/wiki/Lasso_tool" rel="nofollow noreferrer">Photoshop lasso tool</a>.</p>
  
  <p>Its drawing outside view region, which generates incorrect output.
  <img src="https://i.stack.imgur.com/eRxrr.png" alt="Input"></p>
  
  <p><strong>Output</strong>
  <img src="https://i.stack.imgur.com/8HtYf.png" alt="Output"></p>
  
  <p>Code@</p>
  
  <p><em>onDraw</em></p>
  
  <pre><code>public void onDraw(Canvas canvas) {
  
          final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
          canvas.drawBitmap(bitmap, rect, rect, null);
          // RectF r = new RectF();
          // Matrix matrix = new Matrix();
          // matrix.mapRect(r);
          // Log.i(TAG, "Rect " + r.left + " " + r.top + " " + r.right + " " +
          // r.bottom + " ");
          // canvas.clipRect(r.left, r.top, r.right, r.bottom);
  
          Path path = new Path();
          boolean first = true;
  
          for (int i = 0; i &lt; points.size(); i += 2) {
              Point point = points.get(i);
              if (first) {
                  first = false;
                  path.moveTo(point.x, point.y);
              } else if (i &lt; points.size() - 1) {
                  Point next = points.get(i + 1);
                  path.quadTo(point.x, point.y, next.x, next.y);
              } else {
                  mlastpoint = points.get(i);
                  path.lineTo(point.x, point.y);
              }
          }
          canvas.drawPath(path, paint);
      }
  </code></pre>
  
  <p><em>onCrop</em></p>
  
  <pre><code>Bitmap resultingImage = Bitmap.createBitmap(widthOfscreen,heightOfScreen, bitmap1.getConfig());
  
          Canvas canvas = new Canvas(resultingImage);
  
          Paint paint = new Paint();
          paint.setAntiAlias(true);
          Path path = new Path();
          for (int i = 0; i &lt; SomeView.points.size(); i++) {
              path.lineTo(SomeView.points.get(i).x, SomeView.points.get(i).y);
          }
          // path.lineTo(150, 0);
          // path.lineTo(230, 120);
          // path.lineTo(70, 120);
          // path.lineTo(150, 0);
  
          canvas.drawPath(path, paint);
          if(crop){
              paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  
          }else{
              paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
          }
  </code></pre>
  
  <p>suggest me to achieve my goal.</p>