/** * 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) { // 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) { Edge.TOP.setCoordinate(bitmapRect.top); Edge.BOTTOM.setCoordinate(bitmapRect.bottom); final float centerX = getWidth() / 2f; // Limits the aspect ratio to no less than 40 wide or 40 tall final float cropWidth = Math.max( Edge.MIN_CROP_LENGTH_PX, AspectRatioUtil.calculateWidth( Edge.TOP.getCoordinate(), Edge.BOTTOM.getCoordinate(), 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) / (Edge.BOTTOM.getCoordinate() - Edge.TOP.getCoordinate()); final float halfCropWidth = cropWidth / 2f; Edge.LEFT.setCoordinate(centerX - halfCropWidth); Edge.RIGHT.setCoordinate(centerX + halfCropWidth); } else { Edge.LEFT.setCoordinate(bitmapRect.left); Edge.RIGHT.setCoordinate(bitmapRect.right); final float centerY = getHeight() / 2f; // Limits the aspect ratio to no less than 40 wide or 40 tall final float cropHeight = Math.max( Edge.MIN_CROP_LENGTH_PX, AspectRatioUtil.calculateHeight( Edge.LEFT.getCoordinate(), Edge.RIGHT.getCoordinate(), mTargetAspectRatio)); // Create new TargetAspectRatio if the original one does not fit // the screen if (cropHeight == Edge.MIN_CROP_LENGTH_PX) mTargetAspectRatio = (Edge.RIGHT.getCoordinate() - Edge.LEFT.getCoordinate()) / Edge.MIN_CROP_LENGTH_PX; final float halfCropHeight = cropHeight / 2f; Edge.TOP.setCoordinate(centerY - halfCropHeight); Edge.BOTTOM.setCoordinate(centerY + halfCropHeight); } } else { // ... do not fix aspect ratio... // Initialize crop window to have 10% padding w/ respect to image. final float horizontalPadding = 0.1f * bitmapRect.width(); final float verticalPadding = 0.1f * bitmapRect.height(); Edge.LEFT.setCoordinate(bitmapRect.left + horizontalPadding); Edge.TOP.setCoordinate(bitmapRect.top + verticalPadding); Edge.RIGHT.setCoordinate(bitmapRect.right - horizontalPadding); Edge.BOTTOM.setCoordinate(bitmapRect.bottom - verticalPadding); } }