/**
   * Sets the both the X and Y values of the aspectRatio.
   *
   * @param aspectRatioX int that specifies the new X value of the aspect ratio
   * @param aspectRatioX int that specifies the new Y value of the aspect ratio
   */
  public void setAspectRatio(int aspectRatioX, int aspectRatioY) {
    mAspectRatioX = aspectRatioX;
    mCropOverlayView.setAspectRatioX(mAspectRatioX);

    mAspectRatioY = aspectRatioY;
    mCropOverlayView.setAspectRatioY(mAspectRatioY);
  }
  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {

    if (mBitmap != null) {
      final Rect bitmapRect = ImageViewUtil.getBitmapRectCenterInside(mBitmap, this);
      mCropOverlayView.setBitmapRect(bitmapRect);
    } else {
      mCropOverlayView.setBitmapRect(EMPTY_RECT);
    }
  }
  /**
   * Sets a Bitmap as the content of the CropImageView.
   *
   * @param bitmap the Bitmap to set
   */
  public void setImageBitmap(Bitmap bitmap) {

    mBitmap = bitmap;
    mImageView.setImageBitmap(mBitmap);
    if (mCropOverlayView != null) {
      mCropOverlayView.resetCropOverlayView();
    }
  }
  private void init(Context context) {
    Log.i("YJY", "init");
    //        Bundle bundle =new Bundle();
    //        filepath = bundle.getString(PATH);
    //        Log.i("YJY",filepath);
    final LayoutInflater inflater = LayoutInflater.from(context);
    final View v = inflater.inflate(R.layout.crop_image_view, this, true);

    mImageView = (ImageView) v.findViewById(R.id.ImageView_image);

    setImageResource(filepath);
    mCropOverlayView = (CropOverlayView) v.findViewById(R.id.CropOverlayView);
    mCropOverlayView.setInitialAttributeValues(
        mGuidelines, mFixAspectRatio, mAspectRatioX, mAspectRatioY);
  }
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (camera != null) {
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);

      if (cameraRotation == 90 || cameraRotation == 270) {
        int aux = widthMode;
        widthMode = heightMode;
        heightMode = aux;
        aux = widthSize;
        widthSize = heightSize;
        heightSize = aux;
      }

      Size previewSize = null;
      if (widthMode == MeasureSpec.UNSPECIFIED && heightMode == MeasureSpec.UNSPECIFIED) {
        previewSize = previewSizes.get(0); // Choose the biggest preview size
        layoutWidth = previewSize.width;
        layoutHeight = previewSize.height;
      } else if (widthMode == MeasureSpec.UNSPECIFIED) { // heigthMode == EXACTLY or AT_MOST
        previewSize = bestSizeForHeight(previewSizes, heightSize);
        layoutWidth = previewSize.width * heightSize / previewSize.height;
        layoutHeight = heightSize;
      } else if (heightMode == MeasureSpec.UNSPECIFIED) { // widthMode == EXACTLY or AT_MOST
        previewSize = bestSizeForWidth(previewSizes, widthSize);
        layoutWidth = widthSize;
        layoutHeight = previewSize.height * widthSize / previewSize.width;
      } else if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
        if (widthSize * pictureSize.height > heightSize * pictureSize.width)
          previewSize = bestSizeForWidth(previewSizes, widthSize);
        else previewSize = bestSizeForHeight(previewSizes, heightSize);
        layoutWidth = widthSize;
        layoutHeight = heightSize;
      } else if (widthMode == MeasureSpec.EXACTLY) { // heigthMode == AT_MOST
        previewSize = bestSizeForWidth(previewSizes, widthSize);
        layoutWidth = widthSize;
        if (widthSize * pictureSize.height > heightSize * pictureSize.width)
          layoutHeight = heightSize;
        else layoutHeight = pictureSize.height * widthSize / pictureSize.width;
      } else if (heightMode == MeasureSpec.EXACTLY) { // widthMode == AT_MOST
        previewSize = bestSizeForHeight(previewSizes, heightSize);
        if (widthSize * pictureSize.height > heightSize * pictureSize.width)
          layoutWidth = pictureSize.width * heightSize / pictureSize.height;
        else layoutWidth = widthSize;
        layoutHeight = heightSize;
      } else { // widthMode == heightMode == AT_MOST
        if (widthSize * pictureSize.height > heightSize * pictureSize.width) {
          previewSize = bestSizeForHeight(previewSizes, heightSize);
          layoutWidth = pictureSize.width * heightSize / pictureSize.height;
          layoutHeight = heightSize;
        } else {
          previewSize = bestSizeForWidth(previewSizes, widthSize);
          layoutWidth = widthSize;
          layoutHeight = pictureSize.height * widthSize / pictureSize.width;
        }
      }

      if (cameraRotation == 90 || cameraRotation == 270) {
        final int aux = layoutWidth;
        layoutWidth = layoutHeight;
        layoutHeight = aux;
      }

      // TODO Review this...
      final Camera.Parameters params = camera.getParameters();
      if (!previewSize.equals(params.getPreviewSize())) {
        camera.stopPreview();
        params.setPreviewSize(previewSize.width, previewSize.height);
        camera.setParameters(params);
        camera.startPreview();
      }

      cropOverlayBitmapRect.left = 0;
      cropOverlayBitmapRect.top = 0;
      cropOverlayBitmapRect.right = layoutWidth;
      cropOverlayBitmapRect.bottom = layoutHeight;
      cropOverlayView.setBitmapRect(cropOverlayBitmapRect);

      setMeasuredDimension(layoutWidth, layoutHeight);

    } else {
      setMeasuredDimension(widthSize, heightSize);
    }
  }
 public void cropshow() {
   mCropOverlayView.setVisibility(VISIBLE);
 }
 public void cropgone() {
   mCropOverlayView.setVisibility(GONE);
 }
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (mBitmap != null) {

      super.onMeasure(widthMeasureSpec, heightMeasureSpec);

      // Bypasses a baffling bug when used within a ScrollView, where
      // heightSize is set to 0.
      if (heightSize == 0) heightSize = mBitmap.getHeight();

      int desiredWidth;
      int desiredHeight;

      double viewToBitmapWidthRatio = Double.POSITIVE_INFINITY;
      double viewToBitmapHeightRatio = Double.POSITIVE_INFINITY;

      // Checks if either width or height needs to be fixed
      if (widthSize < mBitmap.getWidth()) {
        viewToBitmapWidthRatio = (double) widthSize / (double) mBitmap.getWidth();
      }
      if (heightSize < mBitmap.getHeight()) {
        viewToBitmapHeightRatio = (double) heightSize / (double) mBitmap.getHeight();
      }

      // If either needs to be fixed, choose smallest ratio and calculate
      // from there
      if (viewToBitmapWidthRatio != Double.POSITIVE_INFINITY
          || viewToBitmapHeightRatio != Double.POSITIVE_INFINITY) {
        if (viewToBitmapWidthRatio <= viewToBitmapHeightRatio) {
          desiredWidth = widthSize;
          desiredHeight = (int) (mBitmap.getHeight() * viewToBitmapWidthRatio);
        } else {
          desiredHeight = heightSize;
          desiredWidth = (int) (mBitmap.getWidth() * viewToBitmapHeightRatio);
        }
      }
      // Otherwise, the picture is within frame layout bounds. Desired
      // width is
      // simply picture size
      else {
        desiredWidth = mBitmap.getWidth();
        desiredHeight = mBitmap.getHeight();
      }

      int width = getOnMeasureSpec(widthMode, widthSize, desiredWidth);
      int height = getOnMeasureSpec(heightMode, heightSize, desiredHeight);

      mLayoutWidth = width;
      mLayoutHeight = height;

      final Rect bitmapRect =
          ImageViewUtil.getBitmapRectCenterInside(
              mBitmap.getWidth(), mBitmap.getHeight(), mLayoutWidth, mLayoutHeight);
      mCropOverlayView.setBitmapRect(bitmapRect);

      // MUST CALL THIS
      setMeasuredDimension(mLayoutWidth, mLayoutHeight);

    } else {

      mCropOverlayView.setBitmapRect(EMPTY_RECT);
      setMeasuredDimension(widthSize, heightSize);
    }
  }