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

Add OnSwipeCloseListener #786

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.chrisbanes.photoview;

/**
* A callback to be invoked when swiped up or down
*/
public interface OnSwipeCloseListener {
/**
* A callback while swiping
* @param delta Amount of movement
*/
void onProgress(float delta);

/**
* A callback when the amount of movement exceeds the threshold
*/
void onFinish();

/**
* A callback if the threshold is not exceeded when release user finger
*/
void onCancel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ public float getScale() {
return attacher.getScale();
}

public float getMinCloseThreshold() {
return attacher.getMinCloseThreshold();
}

public void setAllowParentInterceptOnEdge(boolean allow) {
attacher.setAllowParentInterceptOnEdge(allow);
}
Expand Down Expand Up @@ -226,6 +230,10 @@ public void setOnViewDragListener(OnViewDragListener listener) {
attacher.setOnViewDragListener(listener);
}

public void setOnSwipeCloseListener(OnSwipeCloseListener listener) {
attacher.setOnSwipeCloseListener(listener);
}

public void setScale(float scale) {
attacher.setScale(scale);
}
Expand All @@ -238,6 +246,10 @@ public void setScale(float scale, float focalX, float focalY, boolean animate) {
attacher.setScale(scale, focalX, focalY, animate);
}

public void setMinCloseThreshold(float minCloseThreshold) {
attacher.setMinCloseThreshold(minCloseThreshold);
}

public void setZoomTransitionDuration(int milliseconds) {
attacher.setZoomTransitionDuration(milliseconds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewConfiguration;
import android.view.ViewParent;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
Expand All @@ -42,6 +43,7 @@ public class PhotoViewAttacher implements View.OnTouchListener,
private static float DEFAULT_MAX_SCALE = 3.0f;
private static float DEFAULT_MID_SCALE = 1.75f;
private static float DEFAULT_MIN_SCALE = 1.0f;
private static float DEFAULT_MIN_CLOSE_THRESHOLD = 1000.0f;
private static int DEFAULT_ZOOM_DURATION = 200;

private static final int HORIZONTAL_EDGE_NONE = -1;
Expand All @@ -59,6 +61,8 @@ public class PhotoViewAttacher implements View.OnTouchListener,
private float mMinScale = DEFAULT_MIN_SCALE;
private float mMidScale = DEFAULT_MID_SCALE;
private float mMaxScale = DEFAULT_MAX_SCALE;
private float mMinCloseThreshold = DEFAULT_MIN_CLOSE_THRESHOLD;


private boolean mAllowParentInterceptOnEdge = true;
private boolean mBlockParentIntercept = false;
Expand Down Expand Up @@ -86,6 +90,7 @@ public class PhotoViewAttacher implements View.OnTouchListener,
private OnScaleChangedListener mScaleChangeListener;
private OnSingleFlingListener mSingleFlingListener;
private OnViewDragListener mOnViewDragListener;
private OnSwipeCloseListener mOnSwipeCloseListener;

private FlingRunnable mCurrentFlingRunnable;
private int mHorizontalScrollEdge = HORIZONTAL_EDGE_BOTH;
Expand Down Expand Up @@ -385,6 +390,9 @@ public boolean onTouch(View v, MotionEvent ev) {
if (mGestureDetector != null && mGestureDetector.onTouchEvent(ev)) {
handled = true;
}
if(mOnSwipeCloseListener != null) {
swipe(v, ev);
}

}
return handled;
Expand All @@ -409,6 +417,14 @@ public void setMaximumScale(float maximumScale) {
mMaxScale = maximumScale;
}

public float getMinCloseThreshold() {
return mMinCloseThreshold;
}

public void setMinCloseThreshold(float minCloseThreshold) {
mMinCloseThreshold = minCloseThreshold;
}

public void setScaleLevels(float minimumScale, float mediumScale, float maximumScale) {
Util.checkZoomLevels(minimumScale, mediumScale, maximumScale);
mMinScale = minimumScale;
Expand Down Expand Up @@ -444,6 +460,10 @@ public void setOnViewDragListener(OnViewDragListener listener) {
mOnViewDragListener = listener;
}

public void setOnSwipeCloseListener(OnSwipeCloseListener listener) {
mOnSwipeCloseListener = listener;
}

public void setScale(float scale) {
setScale(scale, false);
}
Expand Down Expand Up @@ -725,6 +745,32 @@ private void cancelFling() {
}
}

private void swipe(View v, MotionEvent ev) {
if(getScale() == mMinScale) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// Save Y coordinate position in order to drag the image
v.setTag(ev.getY());
break;
case MotionEvent.ACTION_MOVE:
float delta = ev.getRawY() - (Float) v.getTag();
// If the delta is doubleTapSlop or less, it will not move
if (Math.abs(delta) > ViewConfiguration.get(v.getContext()).getScaledDoubleTapSlop()) {
v.setTranslationY(delta);
mOnSwipeCloseListener.onProgress(delta);
}
break;
case MotionEvent.ACTION_UP:
if (Math.abs(v.getTranslationY()) > mMinCloseThreshold) {
mOnSwipeCloseListener.onFinish();
} else {
mOnSwipeCloseListener.onCancel();
}
break;
}
}
}

private class AnimatedZoomRunnable implements Runnable {

private final float mFocalX, mFocalY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.github.chrisbanes.photoview.sample;

import android.animation.ObjectAnimator;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
Expand All @@ -30,6 +31,7 @@
import com.github.chrisbanes.photoview.OnMatrixChangedListener;
import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.OnSingleFlingListener;
import com.github.chrisbanes.photoview.OnSwipeCloseListener;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.Random;
Expand Down Expand Up @@ -139,6 +141,26 @@ public boolean onMenuItemClick(MenuItem item) {
mPhotoView.setOnMatrixChangeListener(new MatrixChangeListener());
mPhotoView.setOnPhotoTapListener(new PhotoTapListener());
mPhotoView.setOnSingleFlingListener(new SingleFlingListener());
mPhotoView.setOnSwipeCloseListener(new OnSwipeCloseListener() {
@Override
public void onProgress(float delta) {
float threshold = mPhotoView.getMinCloseThreshold();
mPhotoView.setAlpha((threshold - Math.abs(delta))/threshold);
}

@Override
public void onFinish() {
finish();
}

@Override
public void onCancel() {
ObjectAnimator.ofFloat(mPhotoView, "translationY", mPhotoView.getTranslationY(), 0f)
.setDuration(100L)
.start();
mPhotoView.setAlpha(1.0f);
}
});
}

private class PhotoTapListener implements OnPhotoTapListener {
Expand Down