예제 #1
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);
        }
        /**
         * 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();
            }
        }