private void drawBackground(Canvas canvas, Rect bitmapRect) {

    final float left = Edge.LEFT.getCoordinate();
    final float top = Edge.TOP.getCoordinate();
    final float right = Edge.RIGHT.getCoordinate();
    final float bottom = Edge.BOTTOM.getCoordinate();

    /*-
     -------------------------------------
     |                top                |
     -------------------------------------
     |      |                    |       |
     |      |                    |       |
     | left |                    | right |
     |      |                    |       |
     |      |                    |       |
     -------------------------------------
     |              bottom               |
     -------------------------------------
    */

    // Draw "top", "bottom", "left", then "right" quadrants.
    canvas.drawRect(bitmapRect.left, bitmapRect.top, bitmapRect.right, top, mBackgroundPaint);
    canvas.drawRect(bitmapRect.left, bottom, bitmapRect.right, bitmapRect.bottom, mBackgroundPaint);
    canvas.drawRect(bitmapRect.left, top, left, bottom, mBackgroundPaint);
    canvas.drawRect(right, top, bitmapRect.right, bottom, mBackgroundPaint);
  }
 /**
  * Indicates whether the crop window is small enough that the guidelines should be shown. Public
  * because this function is also used to determine if the center handle should be focused.
  *
  * @return boolean Whether the guidelines should be shown or not
  */
 public static boolean showGuidelines() {
   if ((Math.abs(Edge.LEFT.getCoordinate() - Edge.RIGHT.getCoordinate())
           < DEFAULT_SHOW_GUIDELINES_LIMIT)
       || (Math.abs(Edge.TOP.getCoordinate() - Edge.BOTTOM.getCoordinate())
           < DEFAULT_SHOW_GUIDELINES_LIMIT)) return false;
   else return true;
 }
  @Override
  protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    // Draw translucent background for the cropped area.
    drawBackground(canvas, mBitmapRect);

    if (showGuidelines()) {
      // Determines whether guidelines should be drawn or not
      if (mGuidelines == GUIDELINES_ON) {
        drawRuleOfThirdsGuidelines(canvas);
      } else if (mGuidelines == GUIDELINES_ON_TOUCH) {
        // Draw only when resizing
        if (mPressedHandle != null) drawRuleOfThirdsGuidelines(canvas);
      } else if (mGuidelines == GUIDELINES_OFF) {
        // Do nothing
      }
    }

    // Draws the main crop window border.
    canvas.drawRect(
        Edge.LEFT.getCoordinate(),
        Edge.TOP.getCoordinate(),
        Edge.RIGHT.getCoordinate(),
        Edge.BOTTOM.getCoordinate(),
        mBorderPaint);

    // Draws the corners
    drawCorners(canvas);
  }
  private void drawCorners(Canvas canvas) {

    final float left = Edge.LEFT.getCoordinate();
    final float top = Edge.TOP.getCoordinate();
    final float right = Edge.RIGHT.getCoordinate();
    final float bottom = Edge.BOTTOM.getCoordinate();

    // Draws the corner lines

    // Top left
    canvas.drawLine(
        left - mCornerOffset,
        top - mCornerExtension,
        left - mCornerOffset,
        top + mCornerLength,
        mCornerPaint);
    canvas.drawLine(
        left, top - mCornerOffset, left + mCornerLength, top - mCornerOffset, mCornerPaint);

    // Top right
    canvas.drawLine(
        right + mCornerOffset,
        top - mCornerExtension,
        right + mCornerOffset,
        top + mCornerLength,
        mCornerPaint);
    canvas.drawLine(
        right, top - mCornerOffset, right - mCornerLength, top - mCornerOffset, mCornerPaint);

    // Bottom left
    canvas.drawLine(
        left - mCornerOffset,
        bottom + mCornerExtension,
        left - mCornerOffset,
        bottom - mCornerLength,
        mCornerPaint);
    canvas.drawLine(
        left, bottom + mCornerOffset, left + mCornerLength, bottom + mCornerOffset, mCornerPaint);

    // Bottom left
    canvas.drawLine(
        right + mCornerOffset,
        bottom + mCornerExtension,
        right + mCornerOffset,
        bottom - mCornerLength,
        mCornerPaint);
    canvas.drawLine(
        right, bottom + mCornerOffset, right - mCornerLength, bottom + mCornerOffset, mCornerPaint);
  }
  /**
   * Handles a {@link MotionEvent#ACTION_DOWN} event.
   *
   * @param x the x-coordinate of the down action
   * @param y the y-coordinate of the down action
   */
  private void onActionDown(float x, float y) {

    final float left = Edge.LEFT.getCoordinate();
    final float top = Edge.TOP.getCoordinate();
    final float right = Edge.RIGHT.getCoordinate();
    final float bottom = Edge.BOTTOM.getCoordinate();

    mPressedHandle = HandleUtil.getPressedHandle(x, y, left, top, right, bottom, mHandleRadius);

    if (mPressedHandle == null) return;

    // Calculate the offset of the touch point from the precise location
    // of the handle. Save these values in a member variable since we want
    // to maintain this offset as we drag the handle.
    mTouchOffset = HandleUtil.getOffset(mPressedHandle, x, y, left, top, right, bottom);

    invalidate();
  }
  private void drawRuleOfThirdsGuidelines(Canvas canvas) {

    final float left = Edge.LEFT.getCoordinate();
    final float top = Edge.TOP.getCoordinate();
    final float right = Edge.RIGHT.getCoordinate();
    final float bottom = Edge.BOTTOM.getCoordinate();

    // Draw vertical guidelines.
    final float oneThirdCropWidth = Edge.getWidth() / 3;

    final float x1 = left + oneThirdCropWidth;
    canvas.drawLine(x1, top, x1, bottom, mGuidelinePaint);
    final float x2 = right - oneThirdCropWidth;
    canvas.drawLine(x2, top, x2, bottom, mGuidelinePaint);

    // Draw horizontal guidelines.
    final float oneThirdCropHeight = Edge.getHeight() / 3;

    final float y1 = top + oneThirdCropHeight;
    canvas.drawLine(left, y1, right, y1, mGuidelinePaint);
    final float y2 = bottom - oneThirdCropHeight;
    canvas.drawLine(left, y2, right, y2, mGuidelinePaint);
  }
  /**
   * Set the initial crop window size and position. This is dependent on the size and position of
   * the image being cropped.
   *
   * @param bitmapRect the bounding box around the image being cropped
   */
  private void initCropWindow(Rect bitmapRect) {

    // Tells the attribute functions the crop window has already been
    // initialized
    if (initializedCropWindow == false) initializedCropWindow = true;

    if (mFixAspectRatio) {

      // If the image aspect ratio is wider than the crop aspect ratio,
      // then the image height is the determining initial length. Else,
      // vice-versa.
      if (AspectRatioUtil.calculateAspectRatio(bitmapRect) > mTargetAspectRatio) {

        Edge.TOP.setCoordinate(bitmapRect.top);
        Edge.BOTTOM.setCoordinate(bitmapRect.bottom);

        final float centerX = getWidth() / 2f;

        // Limits the aspect ratio to no less than 40 wide or 40 tall
        final float cropWidth =
            Math.max(
                Edge.MIN_CROP_LENGTH_PX,
                AspectRatioUtil.calculateWidth(
                    Edge.TOP.getCoordinate(), Edge.BOTTOM.getCoordinate(), mTargetAspectRatio));

        // Create new TargetAspectRatio if the original one does not fit
        // the screen
        if (cropWidth == Edge.MIN_CROP_LENGTH_PX)
          mTargetAspectRatio =
              (Edge.MIN_CROP_LENGTH_PX) / (Edge.BOTTOM.getCoordinate() - Edge.TOP.getCoordinate());

        final float halfCropWidth = cropWidth / 2f;
        Edge.LEFT.setCoordinate(centerX - halfCropWidth);
        Edge.RIGHT.setCoordinate(centerX + halfCropWidth);

      } else {

        Edge.LEFT.setCoordinate(bitmapRect.left);
        Edge.RIGHT.setCoordinate(bitmapRect.right);

        final float centerY = getHeight() / 2f;

        // Limits the aspect ratio to no less than 40 wide or 40 tall
        final float cropHeight =
            Math.max(
                Edge.MIN_CROP_LENGTH_PX,
                AspectRatioUtil.calculateHeight(
                    Edge.LEFT.getCoordinate(), Edge.RIGHT.getCoordinate(), mTargetAspectRatio));

        // Create new TargetAspectRatio if the original one does not fit
        // the screen
        if (cropHeight == Edge.MIN_CROP_LENGTH_PX)
          mTargetAspectRatio =
              (Edge.RIGHT.getCoordinate() - Edge.LEFT.getCoordinate()) / Edge.MIN_CROP_LENGTH_PX;

        final float halfCropHeight = cropHeight / 2f;
        Edge.TOP.setCoordinate(centerY - halfCropHeight);
        Edge.BOTTOM.setCoordinate(centerY + halfCropHeight);
      }

    } else { // ... do not fix aspect ratio...

      // Initialize crop window to have 10% padding w/ respect to image.
      final float horizontalPadding = 0.1f * bitmapRect.width();
      final float verticalPadding = 0.1f * bitmapRect.height();

      Edge.LEFT.setCoordinate(bitmapRect.left + horizontalPadding);
      Edge.TOP.setCoordinate(bitmapRect.top + verticalPadding);
      Edge.RIGHT.setCoordinate(bitmapRect.right - horizontalPadding);
      Edge.BOTTOM.setCoordinate(bitmapRect.bottom - verticalPadding);
    }
  }