Skip to content

Commit

Permalink
1、回退photoView版本
Browse files Browse the repository at this point in the history
  • Loading branch information
liyu20 committed Jul 12, 2018
1 parent 5cca008 commit 64f6f38
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 160 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
public class PhotoView extends ImageView {

private PhotoViewAttacher attacher;
private ScaleType pendingScaleType;

public PhotoView(Context context) {
this(context, null);
Expand All @@ -58,11 +57,6 @@ private void init() {
//We always pose as a Matrix scale type, though we can change to another scale type
//via the attacher
super.setScaleType(ScaleType.MATRIX);
//apply the previously applied scale type
if (pendingScaleType != null) {
setScaleType(pendingScaleType);
pendingScaleType = null;
}
}

/**
Expand Down Expand Up @@ -98,9 +92,7 @@ public void setOnClickListener(OnClickListener l) {

@Override
public void setScaleType(ScaleType scaleType) {
if (attacher == null) {
pendingScaleType = scaleType;
} else {
if (attacher != null) {
attacher.setScaleType(scaleType);
}
}
Expand Down Expand Up @@ -147,19 +139,10 @@ public void setRotationBy(float rotationDegree) {
attacher.setRotationBy(rotationDegree);
}

@Deprecated
public boolean isZoomEnabled() {
return attacher.isZoomEnabled();
}

public boolean isZoomable() {
return attacher.isZoomable();
}

public void setZoomable(boolean zoomable) {
attacher.setZoomable(zoomable);
}

public RectF getDisplayRect() {
return attacher.getDisplayRect();
}
Expand All @@ -172,14 +155,6 @@ public boolean setDisplayMatrix(Matrix finalRectangle) {
return attacher.setDisplayMatrix(finalRectangle);
}

public void getSuppMatrix(Matrix matrix) {
attacher.getSuppMatrix(matrix);
}

public boolean setSuppMatrix(Matrix matrix) {
return attacher.setDisplayMatrix(matrix);
}

public float getMinimumScale() {
return attacher.getMinimumScale();
}
Expand Down Expand Up @@ -228,14 +203,6 @@ public void setOnOutsidePhotoTapListener(OnOutsidePhotoTapListener listener) {
attacher.setOnOutsidePhotoTapListener(listener);
}

public void setOnViewTapListener(OnViewTapListener listener) {
attacher.setOnViewTapListener(listener);
}

public void setOnViewDragListener(OnViewDragListener listener) {
attacher.setOnViewDragListener(listener);
}

public void setScale(float scale) {
attacher.setScale(scale);
}
Expand All @@ -248,12 +215,16 @@ public void setScale(float scale, float focalX, float focalY, boolean animate) {
attacher.setScale(scale, focalX, focalY, animate);
}

public void setZoomable(boolean zoomable) {
attacher.setZoomable(zoomable);
}

public void setZoomTransitionDuration(int milliseconds) {
attacher.setZoomTransitionDuration(milliseconds);
}

public void setOnDoubleTapListener(GestureDetector.OnDoubleTapListener onDoubleTapListener) {
attacher.setOnDoubleTapListener(onDoubleTapListener);
public void setOnDoubleTapListener(GestureDetector.OnDoubleTapListener newOnDoubleTapListener) {
attacher.setOnDoubleTapListener(newOnDoubleTapListener);
}

public void setOnScaleChangeListener(OnScaleChangedListener onScaleChangedListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* gain the functionality that {@link PhotoView} offers
*/
public class PhotoViewAttacher implements View.OnTouchListener,
OnGestureListener,
View.OnLayoutChangeListener {

private static float DEFAULT_MAX_SCALE = 3.0f;
Expand Down Expand Up @@ -77,12 +78,10 @@ public class PhotoViewAttacher implements View.OnTouchListener,
private OnMatrixChangedListener mMatrixChangeListener;
private OnPhotoTapListener mPhotoTapListener;
private OnOutsidePhotoTapListener mOutsidePhotoTapListener;
private OnViewTapListener mViewTapListener;
private View.OnClickListener mOnClickListener;
private OnLongClickListener mLongClickListener;
private OnScaleChangedListener mScaleChangeListener;
private OnSingleFlingListener mSingleFlingListener;
private OnViewDragListener mOnViewDragListener;

private FlingRunnable mCurrentFlingRunnable;
private int mScrollEdge = EDGE_BOTH;
Expand All @@ -91,64 +90,6 @@ public class PhotoViewAttacher implements View.OnTouchListener,
private boolean mZoomEnabled = true;
private ScaleType mScaleType = ScaleType.FIT_CENTER;

private OnGestureListener onGestureListener = new OnGestureListener() {
@Override
public void onDrag(float dx, float dy) {
if (mScaleDragDetector.isScaling()) {
return; // Do not drag if we are already scaling
}

if (mOnViewDragListener != null) {
mOnViewDragListener.onDrag(dx, dy);
}
mSuppMatrix.postTranslate(dx, dy);
checkAndDisplayMatrix();

/*
* Here we decide whether to let the ImageView's parent to start taking
* over the touch event.
*
* First we check whether this function is enabled. We never want the
* parent to take over if we're scaling. We then check the edge we're
* on, and the direction of the scroll (i.e. if we're pulling against
* the edge, aka 'overscrolling', let the parent take over).
*/
ViewParent parent = mImageView.getParent();
if (mAllowParentInterceptOnEdge && !mScaleDragDetector.isScaling() && !mBlockParentIntercept) {
if (mScrollEdge == EDGE_BOTH
|| (mScrollEdge == EDGE_LEFT && dx >= 1f)
|| (mScrollEdge == EDGE_RIGHT && dx <= -1f)) {
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(false);
}
}
} else {
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
}
}

@Override
public void onFling(float startX, float startY, float velocityX, float velocityY) {
mCurrentFlingRunnable = new FlingRunnable(mImageView.getContext());
mCurrentFlingRunnable.fling(getImageViewWidth(mImageView),
getImageViewHeight(mImageView), (int) velocityX, (int) velocityY);
mImageView.post(mCurrentFlingRunnable);
}

@Override
public void onScale(float scaleFactor, float focusX, float focusY) {
if ((getScale() < mMaxScale || scaleFactor < 1f) && (getScale() > mMinScale || scaleFactor > 1f)) {
if (mScaleChangeListener != null) {
mScaleChangeListener.onScaleChange(scaleFactor, focusX, focusY);
}
mSuppMatrix.postScale(scaleFactor, scaleFactor, focusX, focusY);
checkAndDisplayMatrix();
}
}
};

public PhotoViewAttacher(ImageView imageView) {
mImageView = imageView;
imageView.setOnTouchListener(this);
Expand All @@ -161,7 +102,7 @@ public PhotoViewAttacher(ImageView imageView) {
mBaseRotation = 0.0f;

// Create Gesture Detectors...
mScaleDragDetector = new CustomGestureDetector(imageView.getContext(), onGestureListener);
mScaleDragDetector = new CustomGestureDetector(imageView.getContext(), this);

mGestureDetector = new GestureDetector(imageView.getContext(), new GestureDetector.SimpleOnGestureListener() {

Expand Down Expand Up @@ -200,13 +141,8 @@ public boolean onSingleTapConfirmed(MotionEvent e) {
}
final RectF displayRect = getDisplayRect();

final float x = e.getX(), y = e.getY();

if (mViewTapListener != null) {
mViewTapListener.onViewTap(mImageView, x, y);
}

if (displayRect != null) {
final float x = e.getX(), y = e.getY();

// Check to see if the user tapped on the photo
if (displayRect.contains(x, y)) {
Expand Down Expand Up @@ -270,7 +206,6 @@ public void setOnSingleFlingListener(OnSingleFlingListener onSingleFlingListener
this.mSingleFlingListener = onSingleFlingListener;
}

@Deprecated
public boolean isZoomEnabled() {
return mZoomEnabled;
}
Expand All @@ -290,7 +225,8 @@ public boolean setDisplayMatrix(Matrix finalMatrix) {
}

mSuppMatrix.set(finalMatrix);
checkAndDisplayMatrix();
setImageViewMatrix(getDrawMatrix());
checkMatrixBounds();

return true;
}
Expand Down Expand Up @@ -332,11 +268,63 @@ public ScaleType getScaleType() {
return mScaleType;
}

@Override
public void onDrag(float dx, float dy) {
if (mScaleDragDetector.isScaling()) {
return; // Do not drag if we are already scaling
}

mSuppMatrix.postTranslate(dx, dy);
checkAndDisplayMatrix();

/*
* Here we decide whether to let the ImageView's parent to start taking
* over the touch event.
*
* First we check whether this function is enabled. We never want the
* parent to take over if we're scaling. We then check the edge we're
* on, and the direction of the scroll (i.e. if we're pulling against
* the edge, aka 'overscrolling', let the parent take over).
*/
ViewParent parent = mImageView.getParent();
if (mAllowParentInterceptOnEdge && !mScaleDragDetector.isScaling() && !mBlockParentIntercept) {
if (mScrollEdge == EDGE_BOTH
|| (mScrollEdge == EDGE_LEFT && dx >= 1f)
|| (mScrollEdge == EDGE_RIGHT && dx <= -1f)) {
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(false);
}
}
} else {
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
}
}

@Override
public void onFling(float startX, float startY, float velocityX,
float velocityY) {
mCurrentFlingRunnable = new FlingRunnable(mImageView.getContext());
mCurrentFlingRunnable.fling(getImageViewWidth(mImageView),
getImageViewHeight(mImageView), (int) velocityX, (int) velocityY);
mImageView.post(mCurrentFlingRunnable);
}

@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
// Update our base matrix, as the bounds have changed
if (left != oldLeft || top != oldTop || right != oldRight || bottom != oldBottom) {
updateBaseMatrix(mImageView.getDrawable());
updateBaseMatrix(mImageView.getDrawable());
}

@Override
public void onScale(float scaleFactor, float focusX, float focusY) {
if ((getScale() < mMaxScale || scaleFactor < 1f) && (getScale() > mMinScale || scaleFactor > 1f)) {
if (mScaleChangeListener != null) {
mScaleChangeListener.onScaleChange(scaleFactor, focusX, focusY);
}
mSuppMatrix.postScale(scaleFactor, scaleFactor, focusX, focusY);
checkAndDisplayMatrix();
}
}

Expand Down Expand Up @@ -370,13 +358,6 @@ public boolean onTouch(View v, MotionEvent ev) {
rect.centerX(), rect.centerY()));
handled = true;
}
} else if (getScale() > mMaxScale) {
RectF rect = getDisplayRect();
if (rect != null) {
v.post(new AnimatedZoomRunnable(getScale(), mMaxScale,
rect.centerX(), rect.centerY()));
handled = true;
}
}
break;
}
Expand Down Expand Up @@ -450,14 +431,6 @@ public void setOnOutsidePhotoTapListener(OnOutsidePhotoTapListener mOutsidePhoto
this.mOutsidePhotoTapListener = mOutsidePhotoTapListener;
}

public void setOnViewTapListener(OnViewTapListener listener) {
mViewTapListener = listener;
}

public void setOnViewDragListener(OnViewDragListener listener) {
mOnViewDragListener = listener;
}

public void setScale(float scale) {
setScale(scale, false);
}
Expand Down Expand Up @@ -501,10 +474,6 @@ public void setScaleType(ScaleType scaleType) {
}
}

public boolean isZoomable() {
return mZoomEnabled;
}

public void setZoomable(boolean zoomable) {
mZoomEnabled = zoomable;
update();
Expand Down Expand Up @@ -774,7 +743,7 @@ public void run() {
float scale = mZoomStart + t * (mZoomEnd - mZoomStart);
float deltaScale = scale / getScale();

onGestureListener.onScale(deltaScale, mFocalX, mFocalY);
onScale(deltaScale, mFocalX, mFocalY);

// We haven't hit our target scale yet, so post ourselves again
if (t < 1f) {
Expand Down Expand Up @@ -850,7 +819,7 @@ public void run() {
final int newY = mScroller.getCurrY();

mSuppMatrix.postTranslate(mCurrentX - newX, mCurrentY - newY);
checkAndDisplayMatrix();
setImageViewMatrix(getDrawMatrix());

mCurrentX = newX;
mCurrentY = newY;
Expand Down

0 comments on commit 64f6f38

Please sign in to comment.