// HandleHelper Methods //////////////////////////////////////////////////// public override void UpdateCropWindow(float x, float y, float targetAspectRatio, Rect imageRect, float snapRadius) { // Adjust this Edge accordingly. mEdge.adjustCoordinate(x, y, imageRect, snapRadius, targetAspectRatio); float left = EdgeManager.LEFT.coordinate; float top = EdgeManager.TOP.coordinate; float right = EdgeManager.RIGHT.coordinate; float bottom = EdgeManager.BOTTOM.coordinate; // After this Edge is moved, our crop window is now out of proportion. float targetWidth = AspectRatioUtil.calculateWidth(top, bottom, targetAspectRatio); float currentWidth = right - left; // Adjust the crop window so that it maintains the given aspect ratio by // moving the adjacent edges symmetrically in or out. float difference = targetWidth - currentWidth; float halfDifference = difference / 2; left -= halfDifference; right += halfDifference; EdgeManager.LEFT.coordinate = left; EdgeManager.RIGHT.coordinate = right; // Check if we have gone out of bounds on the sides, and fix. if (EdgeManager.LEFT.isOutsideMargin(imageRect, snapRadius) && !mEdge.isNewRectangleOutOfBounds(EdgeManager.LEFT, imageRect, targetAspectRatio)) { float offset = EdgeManager.LEFT.snapToRect(imageRect); EdgeManager.RIGHT.offset(-offset); mEdge.adjustCoordinate(targetAspectRatio); } if (EdgeManager.RIGHT.isOutsideMargin(imageRect, snapRadius) && !mEdge.isNewRectangleOutOfBounds(EdgeManager.RIGHT, imageRect, targetAspectRatio)) { float offset = EdgeManager.RIGHT.snapToRect(imageRect); EdgeManager.LEFT.offset(-offset); mEdge.adjustCoordinate(targetAspectRatio); } }
/** * 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) { try { // 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) { EdgeManager.TOP.coordinate = bitmapRect.Top; EdgeManager.BOTTOM.coordinate = bitmapRect.Bottom; float centerX = Width / 2f; // Limits the aspect ratio to no less than 40 wide or 40 tall float cropWidth = Math.Max(Edge.MIN_CROP_LENGTH_PX, AspectRatioUtil.calculateWidth(EdgeManager.TOP.coordinate, EdgeManager.BOTTOM.coordinate, 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) / (EdgeManager.BOTTOM.coordinate - EdgeManager.TOP.coordinate); } float halfCropWidth = cropWidth / 2f; EdgeManager.LEFT.coordinate = (centerX - halfCropWidth); EdgeManager.RIGHT.coordinate = (centerX + halfCropWidth); } else { EdgeManager.LEFT.coordinate = bitmapRect.Left; EdgeManager.RIGHT.coordinate = bitmapRect.Right; float centerY = Height / 2f; // Limits the aspect ratio to no less than 40 wide or 40 tall float cropHeight = Math.Max(Edge.MIN_CROP_LENGTH_PX, AspectRatioUtil.calculateHeight(EdgeManager.LEFT.coordinate, EdgeManager.RIGHT.coordinate, mTargetAspectRatio)); // Create new TargetAspectRatio if the original one does not fit // the screen if (cropHeight == Edge.MIN_CROP_LENGTH_PX) { mTargetAspectRatio = (EdgeManager.RIGHT.coordinate - EdgeManager.LEFT.coordinate) / Edge.MIN_CROP_LENGTH_PX; } float halfCropHeight = cropHeight / 2f; EdgeManager.TOP.coordinate = (centerY - halfCropHeight); EdgeManager.BOTTOM.coordinate = (centerY + halfCropHeight); } } else { // ... do not fix aspect ratio... // Initialize crop window to have 10% padding w/ respect to image. float horizontalPadding = 0.1f * bitmapRect.Width(); float verticalPadding = 0.1f * bitmapRect.Height(); EdgeManager.LEFT.coordinate = (bitmapRect.Left + horizontalPadding); EdgeManager.TOP.coordinate = (bitmapRect.Top + verticalPadding); EdgeManager.RIGHT.coordinate = (bitmapRect.Right - horizontalPadding); EdgeManager.BOTTOM.coordinate = (bitmapRect.Bottom - verticalPadding); } } catch (Exception ex) { ex.ToString(); } }