Example #1
0
        /**
         * Adjusts this Edge position such that the resulting window will have the
         * given aspect ratio.
         *
         * @param aspectRatio the aspect ratio to achieve
         */
        public void adjustCoordinate(float aspectRatio)
        {
            float left   = EdgeManager.LEFT.coordinate;
            float top    = EdgeManager.TOP.coordinate;
            float right  = EdgeManager.RIGHT.coordinate;
            float bottom = EdgeManager.BOTTOM.coordinate;

            switch (edgeType)
            {
            case EdgeType.LEFT:
                coordinate = AspectRatioUtil.calculateLeft(top, right, bottom, aspectRatio);
                break;

            case EdgeType.TOP:
                coordinate = AspectRatioUtil.calculateTop(left, right, bottom, aspectRatio);
                break;

            case EdgeType.RIGHT:
                coordinate = AspectRatioUtil.calculateRight(left, top, bottom, aspectRatio);
                break;

            case EdgeType.BOTTOM:
                coordinate = AspectRatioUtil.calculateBottom(left, top, right, aspectRatio);
                break;
            }
        }
Example #2
0
        // Private Methods /////////////////////////////////////////////////////////

        /**
         * Gets the aspect ratio of the resulting crop window if this handle were
         * dragged to the given point.
         *
         * @param x the x-coordinate
         * @param y the y-coordinate
         * @return the aspect ratio
         */

        private float GetAspectRatio(float x, float y)
        {
            // Replace the active edge coordinate with the given touch coordinate.
            float left   = (mVerticalEdge == EdgeManager.LEFT) ? x : EdgeManager.LEFT.coordinate;
            float top    = (mHorizontalEdge == EdgeManager.TOP) ? y : EdgeManager.TOP.coordinate;
            float right  = (mVerticalEdge == EdgeManager.RIGHT) ? x : EdgeManager.RIGHT.coordinate;
            float bottom = (mHorizontalEdge == EdgeManager.BOTTOM) ? y : EdgeManager.BOTTOM.coordinate;

            float aspectRatio = AspectRatioUtil.calculateAspectRatio(left, top, right, bottom);

            return(aspectRatio);
        }
Example #3
0
        // 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);
            }
        }
Example #4
0
        /**
         * Returns whether or not you can re-scale the image based on whether any edge would be out of bounds.
         * Checks all the edges for a possibility of jumping out of bounds.
         *
         * @param Edge the Edge that is about to be expanded
         * @param imageRect the rectangle of the picture
         * @param aspectratio the desired aspectRatio of the picture.
         *
         * @return whether or not the new image would be out of bounds.
         */
        public bool isNewRectangleOutOfBounds(Edge edge, Rect imageRect, float aspectRatio)
        {
            float offset = edge.snapOffset(imageRect);

            switch (edgeType)
            {
            case EdgeType.LEFT:
                if (edge.Equals(EdgeManager.TOP))
                {
                    float top    = imageRect.Top;
                    float bottom = EdgeManager.BOTTOM.coordinate - offset;
                    float right  = EdgeManager.RIGHT.coordinate;
                    float left   = AspectRatioUtil.calculateLeft(top, right, bottom, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                else if (edge.Equals(EdgeManager.BOTTOM))
                {
                    float bottom = imageRect.Bottom;
                    float top    = EdgeManager.TOP.coordinate - offset;
                    float right  = EdgeManager.RIGHT.coordinate;
                    float left   = AspectRatioUtil.calculateLeft(top, right, bottom, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                break;

            case EdgeType.TOP:
                if (edge.Equals(EdgeManager.LEFT))
                {
                    float left   = imageRect.Left;
                    float right  = EdgeManager.RIGHT.coordinate - offset;
                    float bottom = EdgeManager.BOTTOM.coordinate;
                    float top    = AspectRatioUtil.calculateTop(left, right, bottom, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                else if (edge.Equals(EdgeManager.RIGHT))
                {
                    float right  = imageRect.Right;
                    float left   = EdgeManager.LEFT.coordinate - offset;
                    float bottom = EdgeManager.BOTTOM.coordinate;
                    float top    = AspectRatioUtil.calculateTop(left, right, bottom, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                break;

            case EdgeType.RIGHT:
                if (edge.Equals(EdgeManager.TOP))
                {
                    float top    = imageRect.Top;
                    float bottom = EdgeManager.BOTTOM.coordinate - offset;
                    float left   = EdgeManager.LEFT.coordinate;
                    float right  = AspectRatioUtil.calculateRight(left, top, bottom, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                else if (edge.Equals(EdgeManager.BOTTOM))
                {
                    float bottom = imageRect.Bottom;
                    float top    = EdgeManager.TOP.coordinate - offset;
                    float left   = EdgeManager.LEFT.coordinate;
                    float right  = AspectRatioUtil.calculateRight(left, top, bottom, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                break;


            case EdgeType.BOTTOM:
                if (edge.Equals(EdgeManager.LEFT))
                {
                    float left   = imageRect.Left;
                    float right  = EdgeManager.RIGHT.coordinate - offset;
                    float top    = EdgeManager.TOP.coordinate;
                    float bottom = AspectRatioUtil.calculateBottom(left, top, right, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                else if (edge.Equals(EdgeManager.RIGHT))
                {
                    float right  = imageRect.Right;
                    float left   = EdgeManager.LEFT.coordinate - offset;
                    float top    = EdgeManager.TOP.coordinate;
                    float bottom = AspectRatioUtil.calculateBottom(left, top, right, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                break;
            }
            return(true);
        }
        /**
         * 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();
            }
        }