Example #1
0
        // HandleHelper Methods ////////////////////////////////////////////////////

        public override void UpdateCropWindow(float x,
                                              float y,
                                              float targetAspectRatio,
                                              Rect imageRect,
                                              float snapRadius)
        {
            // Adjust this EdgeType accordingly.
            mEdgeType.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 EdgeType is moved, our crop window is now out of proportion.
            float targetHeight  = AspectRatioUtil.calculateHeight(left, right, targetAspectRatio);
            float currentHeight = bottom - top;

            // Adjust the crop window so that it maintains the given aspect ratio by
            // moving the adjacent EdgeTypes symmetrically in or out.
            float difference     = targetHeight - currentHeight;
            float halfDifference = difference / 2;

            top    -= halfDifference;
            bottom += halfDifference;

            EdgeManager.TOP.coordinate    = top;
            EdgeManager.BOTTOM.coordinate = bottom;

            // Check if we have gone out of bounds on the top or bottom, and fix.
            if (EdgeManager.TOP.isOutsideMargin(imageRect, snapRadius) &&
                !mEdgeType.isNewRectangleOutOfBounds(EdgeManager.TOP,
                                                     imageRect,
                                                     targetAspectRatio))
            {
                float offset = EdgeManager.TOP.snapToRect(imageRect);
                EdgeManager.BOTTOM.offset(-offset);
                mEdgeType.adjustCoordinate(targetAspectRatio);
            }
            if (EdgeManager.BOTTOM.isOutsideMargin(imageRect, snapRadius) &&
                !mEdgeType.isNewRectangleOutOfBounds(EdgeManager.BOTTOM,
                                                     imageRect,
                                                     targetAspectRatio))
            {
                float offset = EdgeManager.BOTTOM.snapToRect(imageRect);
                EdgeManager.TOP.offset(-offset);
                mEdgeType.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();
            }
        }