Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Expose image state. #236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions ucrop/src/main/java/com/yalantis/ucrop/view/CropImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ public void cropAndSaveImage(@NonNull Bitmap.CompressFormat compressFormat, int
cancelAllAnimations();
setImageToWrapCropBounds(false);

final ImageState imageState = new ImageState(
mCropRect, RectUtils.trapToRect(mCurrentImageCorners),
getCurrentScale(), getCurrentAngle());
final ImageState imageState = getImageState();

final CropParameters cropParameters = new CropParameters(
mMaxResultImageSizeX, mMaxResultImageSizeY,
Expand All @@ -85,6 +83,13 @@ public void cropAndSaveImage(@NonNull Bitmap.CompressFormat compressFormat, int
new BitmapCropTask(getViewBitmap(), imageState, cropParameters, cropCallback).execute();
}

/***
* @return Return the current state of image include scale, angle, crop rect, and image rect.
*/
public ImageState getImageState() {
return new ImageState(mCropRect, RectUtils.trapToRect(mCurrentImageCorners), getCurrentScale(), getCurrentAngle());
}

/**
* @return - maximum scale value for current image and crop ratio
*/
Expand All @@ -106,20 +111,6 @@ public float getTargetAspectRatio() {
return mTargetAspectRatio;
}

/**
* Updates current crop rectangle with given. Also recalculates image properties and position
* to fit new crop rectangle.
*
* @param cropRect - new crop rectangle
*/
public void setCropRect(RectF cropRect) {
mTargetAspectRatio = cropRect.width() / cropRect.height();
mCropRect.set(cropRect.left - getPaddingLeft(), cropRect.top - getPaddingTop(),
cropRect.right - getPaddingRight(), cropRect.bottom - getPaddingBottom());
calculateImageScaleBounds();
setImageToWrapCropBounds();
}

/**
* This method sets aspect ratio for crop bounds.
* If {@link #SOURCE_IMAGE_ASPECT_RATIO} value is passed - aspect ratio is calculated
Expand All @@ -145,6 +136,20 @@ public void setTargetAspectRatio(float targetAspectRatio) {
}
}

/**
* Updates current crop rectangle with given. Also recalculates image properties and position
* to fit new crop rectangle.
*
* @param cropRect - new crop rectangle
*/
public void setCropRect(RectF cropRect) {
mTargetAspectRatio = cropRect.width() / cropRect.height();
mCropRect.set(cropRect.left - getPaddingLeft(), cropRect.top - getPaddingTop(),
cropRect.right - getPaddingRight(), cropRect.bottom - getPaddingBottom());
calculateImageScaleBounds();
setImageToWrapCropBounds();
}

@Nullable
public CropBoundsChangeListener getCropBoundsChangeListener() {
return mCropBoundsChangeListener;
Expand Down Expand Up @@ -303,7 +308,7 @@ public void setImageToWrapCropBounds(boolean animate) {
final float[] currentImageSides = RectUtils.getRectSidesFromCorners(mCurrentImageCorners);

deltaScale = Math.max(tempCropRect.width() / currentImageSides[0],
tempCropRect.height() / currentImageSides[1]);
tempCropRect.height() / currentImageSides[1]);
deltaScale = deltaScale * currentScale - currentScale;
}

Expand Down Expand Up @@ -443,7 +448,7 @@ protected void zoomImageToPosition(float scale, float centerX, float centerY, lo
final float deltaScale = scale - oldScale;

post(mZoomImageToPositionRunnable = new ZoomImageToPosition(CropImageView.this,
durationMs, oldScale, deltaScale, centerX, centerY));
durationMs, oldScale, deltaScale, centerX, centerY));
}

private void calculateImageScaleBounds() {
Expand Down