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

Listeners #84

Open
wants to merge 4 commits 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
50 changes: 50 additions & 0 deletions zoomage/src/main/java/com/jsibbold/zoomage/OnGestureListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.jsibbold.zoomage;

import android.view.GestureDetector;
import android.view.MotionEvent;

/**
* Default implementation of GestureListener for {@link ZoomageView}.
* If you want to change it in {@link ZoomageView} - override method {@link ZoomageView#getOnGestureListener()}
* and provide your own implementation
*/
public class OnGestureListener extends GestureDetector.SimpleOnGestureListener {

private final ZoomageView view;

public OnGestureListener(ZoomageView view) {
this.view = view;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
if (view != null && e.getAction() == MotionEvent.ACTION_UP) {
view.setDoubleTapDetected(true);
}
notifySingleTapDetection(true);
return false;
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
notifySingleTapDetection(true);
return false;
}

private void notifySingleTapDetection(boolean detected) {
if (view != null) {
view.setSingleTapDetected(detected);
}
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
notifySingleTapDetection(false);
return false;
}

@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.jsibbold.zoomage;

/**
* Listener, that provides updates about scale changes in {@link ZoomageView}
*/
public interface OnScaleChangeListener {
void onScaleChanged(float newScale);
}
52 changes: 27 additions & 25 deletions zoomage/src/main/java/com/jsibbold/zoomage/ZoomageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public class ZoomageView extends AppCompatImageView implements OnScaleGestureLis
private GestureDetector gestureDetector;
private boolean doubleTapDetected = false;
private boolean singleTapDetected = false;
private OnScaleChangeListener onScaleChangeListener;

public ZoomageView(Context context) {
super(context);
Expand All @@ -107,7 +108,7 @@ public ZoomageView(Context context, AttributeSet attrs, int defStyle) {

private void init(Context context, AttributeSet attrs) {
scaleDetector = new ScaleGestureDetector(context, this);
gestureDetector = new GestureDetector(context, gestureListener);
gestureDetector = new GestureDetector(context, getOnGestureListener());
ScaleGestureDetectorCompat.setQuickScaleEnabled(scaleDetector, false);
startScaleType = getScaleType();

Expand All @@ -129,6 +130,10 @@ private void init(Context context, AttributeSet attrs) {
values.recycle();
}

protected GestureDetector.OnGestureListener getOnGestureListener() {
return new OnGestureListener(this);
}

private void verifyScaleRange() {
if (minScale >= maxScale) {
throw new IllegalStateException("minScale must be less than maxScale");
Expand Down Expand Up @@ -500,6 +505,7 @@ public boolean onTouchEvent(MotionEvent event) {
if (allowZoom(event)) {
matrix.postScale(scaleBy, scaleBy, focusx, focusy);
currentScaleFactor = matrixValues[Matrix.MSCALE_X] / startValues[Matrix.MSCALE_X];
notifyScaleChangeListener(currentScaleFactor);
}

setImageMatrix(matrix);
Expand Down Expand Up @@ -657,6 +663,10 @@ public void onAnimationUpdate(ValueAnimator animation) {
@Override
public void onAnimationEnd(Animator animation) {
setImageMatrix(targetMatrix);
if (currentScaleFactor < 1F) {
currentScaleFactor = 1F;
}
notifyScaleChangeListener(currentScaleFactor);
}
});

Expand Down Expand Up @@ -868,35 +878,27 @@ public void onScaleEnd(ScaleGestureDetector detector) {
scaleBy = 1f;
}

private final GestureDetector.OnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_UP) {
doubleTapDetected = true;
}
public OnScaleChangeListener getOnScaleChangeListener() {
return onScaleChangeListener;
}

singleTapDetected = false;
public void setOnScaleChangeListener(OnScaleChangeListener onScaleChangeListener) {
this.onScaleChangeListener = onScaleChangeListener;
}

return false;
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
singleTapDetected = true;
return false;
private void notifyScaleChangeListener(float newScale) {
if (onScaleChangeListener != null) {
onScaleChangeListener.onScaleChanged(newScale);
}
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
singleTapDetected = false;
return false;
}
void setDoubleTapDetected(boolean detected) {
doubleTapDetected = detected;
}

@Override
public boolean onDown(MotionEvent e) {
return true;
}
};
void setSingleTapDetected(boolean detected) {
singleTapDetected = detected;
}

private class SimpleAnimatorListener implements Animator.AnimatorListener {
@Override
Expand Down