Code example for Matrix

Methods: invert, mapRect, preScale, preTranslate, reset

0
 
    transBound.left = bound.left;
    transBound.right = bound.right;
    transBound.top = bound.top;
    transBound.bottom = bound.bottom;
    orgMatrix.mapRect(transBound);
 
    shadePathIn.reset();
    shadePathIn.addRect(transBound, Path.Direction.CW);
    shadePath.reset();
    shadePath.addRect(transFullBound, Path.Direction.CW);
    shadePath.addPath(shadePathIn);
    shadePath.setFillType(Path.FillType.EVEN_ODD);
 
  } 
 
  public void setCropBound(RectF bound) {
    this.bound.left = bound.left;
    this.bound.right = bound.right;
    this.bound.bottom = bound.bottom;
    this.bound.top = bound.top;
    fixBound(CORNER_BR);
    postInvalidate();
  } 
 
  public RectF getCropBound() {
    return new RectF(bound);
  } 
 
  public void setThumbnail(Bitmap thumbnail) {
    this.thumbnail = thumbnail;
    invalidateDims(getWidth(), getHeight());
  } 
 
  public Bitmap getThumbnail() {
    return thumbnail;
  } 
 
  @Override 
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.save();
 
    canvas.concat(thumbMatrix);
    canvas.drawBitmap(thumbnail, 0, 0, thumbPaint);
 
    canvas.restore();
 
    canvas.drawPath(shadePath, shadePaint);
    canvas.drawRect(transBound, framePaint);
 
    if (!dragging) {
 
      canvas.save();
      canvas.translate(transBound.left, transBound.top);
      canvas.drawPath(handlePath, handlePaint);
      canvas.restore();
 
      canvas.save();
      canvas.translate(transBound.left, transBound.bottom);
      canvas.drawPath(handlePath, handlePaint);
      canvas.restore();
 
      canvas.save();
      canvas.translate(transBound.right, transBound.top);
      canvas.drawPath(handlePath, handlePaint);
      canvas.restore();
 
      canvas.save();
      canvas.translate(transBound.right, transBound.bottom);
      canvas.drawPath(handlePath, handlePaint);
      canvas.restore();
    } 
  } 
 
  @Override 
  public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX(), y = event.getY();
    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
      float cx = 0, cy = 0;
      draggedCorner = -1;
      if (getDist(x, y, transBound.left, transBound.top) < ACTIVE_TOUCH_RADIUS) {
        draggedCorner = CORNER_TL;
        cx = transBound.left;
        cy = transBound.top;
      } else if (getDist(x, y, transBound.right, transBound.top) < ACTIVE_TOUCH_RADIUS) {
        draggedCorner = CORNER_TR;
        cx = transBound.right;
        cy = transBound.top;
      } else if (getDist(x, y, transBound.left, transBound.bottom) < ACTIVE_TOUCH_RADIUS) {
        draggedCorner = CORNER_BL;
        cx = transBound.left;
        cy = transBound.bottom;
      } else if (getDist(x, y, transBound.right, transBound.bottom) < ACTIVE_TOUCH_RADIUS) {
        draggedCorner = CORNER_BR;
        cx = transBound.right;
        cy = transBound.bottom;
      } else if (x >= transBound.left && x <= transBound.right
          && y >= transBound.top && y <= transBound.bottom) {
        draggedCorner = CORNER_MID;
        cx = transBound.left;
        cy = transBound.top;
      } 
      if (draggedCorner != -1) {
        dragOffset.x = cx - x;
        dragOffset.y = cy - y;
        dragging = true;
        invalidate();
      } 
    } else if (event.getActionMasked() == MotionEvent.ACTION_UP) {
      dragging = false;
      invalidate();
    } else if (dragging && event.getActionMasked() == MotionEvent.ACTION_MOVE) {
      tmpPoint[0] = x + dragOffset.x;
      tmpPoint[1] = y + dragOffset.y;
      orgMatrixInverse.mapPoints(tmpPoint);
      if (draggedCorner == CORNER_TL) {
        bound.left = tmpPoint[0];
        bound.top = tmpPoint[1];
      } else if (draggedCorner == CORNER_TR) {
        bound.right = tmpPoint[0];
        bound.top = tmpPoint[1];
      } else if (draggedCorner == CORNER_BL) {
        bound.left = tmpPoint[0];
        bound.bottom = tmpPoint[1];
      } else if (draggedCorner == CORNER_BR) {
        bound.right = tmpPoint[0];
        bound.bottom = tmpPoint[1];
      } else if (draggedCorner == CORNER_MID) {
        float w = bound.right - bound.left;
        float h = bound.bottom - bound.top;
        bound.left = tmpPoint[0];
        bound.top = tmpPoint[1];
        bound.right = bound.left + w;
        bound.bottom = bound.top + h;
      } 
      fixBound(draggedCorner);
      invalidate();
    } 
    return true; 
  } 
 
  public int getOrgWidth() { 
    return orgWidth;
  } 
 
  public void setOrgWidth(int orgWidth) {
    this.orgWidth = orgWidth;
    invalidateDims(getWidth(), getHeight());
  } 
 
  public int getOrgHeight() { 
    return orgHeight;
  } 
 
  public void setOrgHeight(int orgHeight) {
    this.orgHeight = orgHeight;
    invalidateDims(getWidth(), getHeight());
  } 
 
  private void invalidateDims(int w, int h) {
 
    if (thumbnail == null)
      return; 
 
    int fw = w - PADDING * 2;
    int fh = h - PADDING * 2;
    float scale = 1;
    scale = (float) fw / thumbnail.getWidth();
    if (scale * thumbnail.getHeight() > fh)
      scale = (float) fh / thumbnail.getHeight();
    thumbMatrix.reset();
    thumbMatrix.preTranslate(
        (float) fw / 2 - (float) (scale * thumbnail.getWidth()) / 2 + PADDING,
        (float) fh / 2 - (float) (scale * thumbnail.getHeight()) / 2 + PADDING);
    thumbMatrix.preScale(scale, scale);
 
    scale = scale * thumbnail.getWidth() / orgWidth;
    orgMatrix.reset();
    orgMatrix.preTranslate((float) fw / 2 - (float) (scale * orgWidth) / 2
        + PADDING, (float) fh / 2 - (float) (scale * orgHeight) / 2 + PADDING);
    orgMatrix.preScale(scale, scale);
    orgMatrix.invert(orgMatrixInverse);
 
    transFullBound.left = 0;
    transFullBound.right = w;
    transFullBound.top = 0;
    transFullBound.bottom = h;