Skip to content

Commit

Permalink
Fixed issue where the image moves to left or top
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrambhia committed Dec 27, 2017
1 parent fbc3394 commit d72652e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.
36 changes: 36 additions & 0 deletions nocropper/src/main/java/com/fenchtose/nocropper/BitmapResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.fenchtose.nocropper;

import android.graphics.Bitmap;

public class BitmapResult {

private final Bitmap bitmap;
private final State state;

private BitmapResult(Bitmap bitmap, State state) {
this.bitmap = bitmap;
this.state = state;
}

public Bitmap getBitmap() {
return bitmap;
}

public State getState() {
return state;
}

static BitmapResult GestureFailure() {
return new BitmapResult(null, State.FAILURE_GESTURE_IN_PROCESS);
}

static BitmapResult success(Bitmap bitmap) {
return new BitmapResult(bitmap, State.SUCCESS);
}

public enum State {
STARTED,
SUCCESS,
FAILURE_GESTURE_IN_PROCESS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CropperImageView extends ImageView {
private float mMinZoom = 0;
private float mMaxZoom = 0;
private float mBaseZoom = 0;
private float mBaseZoomBigger = 0;

private float mFocusX;
private float mFocusY;
Expand Down Expand Up @@ -155,14 +156,20 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mBaseZoom = (float) (right - left) / Math.max(drawable.getIntrinsicHeight(),
drawable.getIntrinsicWidth());
mBaseZoomBigger = (float) (right - left) / Math.min(drawable.getIntrinsicHeight(),
drawable.getIntrinsicWidth());
} else {
mBaseZoom = (float) (bottom - top) / Math.max(drawable.getIntrinsicHeight(),
drawable.getIntrinsicWidth());

mBaseZoomBigger = (float) (bottom - top) / Math.min(drawable.getIntrinsicHeight(),
drawable.getIntrinsicWidth());
}

if (isMaxZoomSet && mBaseZoom > mMaxZoom) {
// base zoom should not be greater than max zoom.
mBaseZoom = mMaxZoom;
mBaseZoomBigger = mMaxZoom;
if (mMinZoom > mMaxZoom) {
Log.e(TAG, "min zoom is greater than max zoom. Changing min zoom = max zoom");
_setMinZoom(mMaxZoom);
Expand Down Expand Up @@ -388,7 +395,7 @@ private boolean onUp() {
Log.i(TAG, "h diff: " + (scaleY * drawable.getIntrinsicHeight() + ty - getHeight()));
}

if (scaleX < mMinZoom || (scaleX <= mMinZoom && mMinZoom >= mBaseZoom)) {
if (scaleX < mMinZoom && mMinZoom >= mBaseZoom) {
if (DEBUG) {
Log.i(TAG, "set scale to min zoom: " + mMinZoom);
}
Expand Down Expand Up @@ -437,7 +444,7 @@ private boolean onUp() {
}
}
return true;
} else if (scaleX <= mBaseZoom) {
} else if (scaleX <= mBaseZoom || scaleX <= mBaseZoomBigger) {

// align to center for the smaller dimension
int h = drawable.getIntrinsicHeight();
Expand Down
30 changes: 27 additions & 3 deletions nocropper/src/main/java/com/fenchtose/nocropper/CropperView.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class CropperView extends FrameLayout {
private CropperGridView mGridView;

private boolean gestureEnabled = true;
private boolean isGestureInProgess = false;

private GridCallback gridCallback;

Expand Down Expand Up @@ -97,17 +98,38 @@ public void setMaxZoom(float zoom) {
mImageView.setMaxZoom(zoom);
}

public Bitmap getCroppedBitmap() throws OutOfMemoryError {
/**
* Crop bitmap in sync
* @return {@link BitmapResult} may contain null bitmap if it's not a success. If this method is called when
* user is still using the gesture (scrolling, panning, etc), it would return result with state {@link BitmapResult.State.FAILURE_GESTURE_IN_PROCESS}
* @throws OutOfMemoryError
*/
public BitmapResult getCroppedBitmap() throws OutOfMemoryError {
if (isGestureInProgess) {
return BitmapResult.GestureFailure();
}

try {
return mImageView.cropBitmap();
return BitmapResult.success(mImageView.cropBitmap());
} catch (OutOfMemoryError e) {
throw e;
}
}

public void getCroppedBitmapAsync(final CropperCallback callback) {
/**
* Crop bitmap async
* @param callback {@link CropperCallback}
* @return {@link BitmapResult.State.STARTED} if cropping will start else {@link BitmapResult.State.FAILURE_GESTURE_IN_PROCESS}
* if cropping can not be started because the user is in the middle of a gesture.
*/
public BitmapResult.State getCroppedBitmapAsync(final CropperCallback callback) {
if (isGestureInProgess) {
return BitmapResult.State.FAILURE_GESTURE_IN_PROCESS;
}

CropperTask task = new CropperTask(callback);
task.execute(mImageView);
return BitmapResult.State.STARTED;
}

public boolean isPreScaling() {
Expand Down Expand Up @@ -183,11 +205,13 @@ private class TouchGestureCallback implements CropperImageView.GestureCallback {

@Override
public void onGestureStarted() {
isGestureInProgess = true;
mGridView.setShowGrid(gridCallback == null || gridCallback.onGestureStarted());
}

@Override
public void onGestureCompleted() {
isGestureInProgess = false;
mGridView.setShowGrid(gridCallback != null && gridCallback.onGestureCompleted());
}
}
Expand Down
16 changes: 14 additions & 2 deletions sample/src/main/java/com/fenctose/imagecropper/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.view.ViewTreeObserver;
import android.widget.Toast;

import com.fenchtose.nocropper.BitmapResult;
import com.fenchtose.nocropper.CropperCallback;
import com.fenchtose.nocropper.CropperView;

Expand Down Expand Up @@ -176,7 +177,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}

private void cropImageAsync() {
mImageView.getCroppedBitmapAsync(new CropperCallback() {
BitmapResult.State state = mImageView.getCroppedBitmapAsync(new CropperCallback() {
@Override
public void onCropped(Bitmap bitmap) {
if (bitmap != null) {
Expand All @@ -194,11 +195,22 @@ public void onOutOfMemoryError() {

}
});

if (state == BitmapResult.State.FAILURE_GESTURE_IN_PROCESS) {
Toast.makeText(this, "unable to crop. Gesture in progress", Toast.LENGTH_SHORT).show();
}
}

private void cropImage() {

Bitmap bitmap = mImageView.getCroppedBitmap();
BitmapResult result = mImageView.getCroppedBitmap();

if (result.getState() == BitmapResult.State.FAILURE_GESTURE_IN_PROCESS) {
Toast.makeText(this, "unable to crop. Gesture in progress", Toast.LENGTH_SHORT).show();
return;
}

Bitmap bitmap = result.bitmap;

if (bitmap != null) {

Expand Down

0 comments on commit d72652e

Please sign in to comment.