fix PullToZoomView

androidx
Ztiany 5 years ago
parent f8acbd27ac
commit d6b0514f4c
  1. 27
      lib_base/src/main/java/com/android/base/widget/pulltozoom/PullToZoomScrollView.java

@ -13,10 +13,9 @@ import android.view.animation.DecelerateInterpolator;
import com.android.base.R; import com.android.base.R;
import timber.log.Timber;
/** /**
* usage:
* <pre> * <pre>
* {@code * {@code
* <com.android.base.widget.pulltozoom.PullToZoomScrollView * <com.android.base.widget.pulltozoom.PullToZoomScrollView
@ -94,8 +93,10 @@ public class PullToZoomScrollView extends NestedScrollView {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PullToZoomScrollView); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PullToZoomScrollView);
mZoomViewId = typedArray.getResourceId(R.styleable.PullToZoomScrollView_psv_zoom_view, -1); mZoomViewId = typedArray.getResourceId(R.styleable.PullToZoomScrollView_psv_zoom_view, -1);
mContainerViewId = typedArray.getResourceId(R.styleable.PullToZoomScrollView_psv_header_view, -1); mContainerViewId = typedArray.getResourceId(R.styleable.PullToZoomScrollView_psv_header_view, -1);
mDamp = typedArray.getInteger(R.styleable.PullToZoomScrollView_psv_damp, 2);//默认阻尼2 //默认阻尼2
mMaxZoomHeight = typedArray.getInteger(R.styleable.PullToZoomScrollView_psv_max_over, dpToPx(1500));//默认最大scroll 1500DP mDamp = typedArray.getInteger(R.styleable.PullToZoomScrollView_psv_damp, 2);
//默认最大scroll 1500DP
mMaxZoomHeight = typedArray.getInteger(R.styleable.PullToZoomScrollView_psv_max_over, dpToPx(1500));
mZoomFactory = typedArray.getFloat(R.styleable.PullToZoomScrollView_psv_zoom_factory, 2.0F); mZoomFactory = typedArray.getFloat(R.styleable.PullToZoomScrollView_psv_zoom_factory, 2.0F);
typedArray.recycle(); typedArray.recycle();
@ -126,8 +127,8 @@ public class PullToZoomScrollView extends NestedScrollView {
getInnerViewHeight(); getInnerViewHeight();
} }
private boolean hasInnerView() { private boolean innerViewInitialized() {
return mContainerView != null && mZoomView != null; return mContainerView != null && mZoomView != null && mOriginContainerViewHeight != 0 && mOriginZoomViewHeight != 0;
} }
@Override @Override
@ -145,22 +146,19 @@ public class PullToZoomScrollView extends NestedScrollView {
private boolean processOverScrollBy(int deltaX, int deltaY, int scrollX, private boolean processOverScrollBy(int deltaX, int deltaY, int scrollX,
int scrollY, int scrollRangeX, int scrollRangeY, int scrollY, int scrollRangeX, int scrollRangeY,
int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) { int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
if (!hasInnerView()) { if (!innerViewInitialized()) {
return false; return false;
} }
if (mContainerView.getHeight() <= mMaxZoomHeight && isTouchEvent) { if (mContainerView.getHeight() <= mMaxZoomHeight && isTouchEvent) {
if (deltaY < 0) { if (deltaY < 0) {
int offset = (int) (deltaY * 1.0F / mDamp); int offset = (int) (deltaY * 1.0F / mDamp);
if (mContainerView.getHeight() - offset >= mOriginContainerViewHeight) { if (mContainerView.getHeight() - offset >= mOriginContainerViewHeight) {
int height = mContainerView int height = mContainerView.getHeight() - offset < mMaxZoomHeight ? mContainerView.getHeight() - offset : mMaxZoomHeight;
.getHeight() - offset < mMaxZoomHeight ?
mContainerView.getHeight() - offset : mMaxZoomHeight;
setContainerHeight(height); setContainerHeight(height);
} }
} else { } else {
if (mContainerView.getHeight() > mOriginContainerViewHeight) { if (mContainerView.getHeight() > mOriginContainerViewHeight) {
int height = mContainerView.getHeight() - deltaY > mOriginContainerViewHeight ? int height = mContainerView.getHeight() - deltaY > mOriginContainerViewHeight ? mContainerView.getHeight() - deltaY : mOriginContainerViewHeight;
mContainerView.getHeight() - deltaY : mOriginContainerViewHeight;
setContainerHeight(height); setContainerHeight(height);
return true; return true;
} }
@ -172,7 +170,7 @@ public class PullToZoomScrollView extends NestedScrollView {
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
@Override @Override
public boolean onTouchEvent(MotionEvent ev) { public boolean onTouchEvent(MotionEvent ev) {
if (hasInnerView()) { if (innerViewInitialized()) {
int action = ev.getAction(); int action = ev.getAction();
if (action == MotionEvent.ACTION_DOWN) { if (action == MotionEvent.ACTION_DOWN) {
cancelAnim(); cancelAnim();
@ -231,9 +229,8 @@ public class PullToZoomScrollView extends NestedScrollView {
mZoomView.setPivotY(mOriginContainerViewHeight / 3F); mZoomView.setPivotY(mOriginContainerViewHeight / 3F);
float addOffset = (height - mOriginContainerViewHeight) * mZoomFactory / mOriginContainerViewHeight; float addOffset = (height - mOriginContainerViewHeight) * mZoomFactory / mOriginContainerViewHeight;
float scale = height * 1.0F / mOriginContainerViewHeight + addOffset; float scale = height * 1.0F / mOriginContainerViewHeight + addOffset;
Timber.d("scale = %f", scale);
if (!Float.isInfinite(scale) && !Float.isNaN(scale)) { if (!Float.isInfinite(scale) && !Float.isNaN(scale) && scale >= 1F) {
mZoomView.setScaleX(scale); mZoomView.setScaleX(scale);
mZoomView.setScaleY(scale); mZoomView.setScaleY(scale);
} else { } else {

Loading…
Cancel
Save