feat:optimize ProgressRotation

This commit is contained in:
pengfei.zhou 2019-11-25 20:50:08 +08:00
parent 055699d221
commit c766e57c83

View File

@ -96,8 +96,6 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
private float mInitialDownY; private float mInitialDownY;
private boolean mIsBeingDragged; private boolean mIsBeingDragged;
private int mActivePointerId = INVALID_POINTER; private int mActivePointerId = INVALID_POINTER;
// Whether this item is scaled up rather than clipped
boolean mScale;
// Target is returning to its start offset because it was cancelled or a // Target is returning to its start offset because it was cancelled or a
// refresh was triggered. // refresh was triggered.
@ -185,11 +183,7 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
mRefreshView.setVisibility(View.GONE); mRefreshView.setVisibility(View.GONE);
// Return the circle to its start position // Return the circle to its start position
if (mScale) {
setAnimationProgress(0 /* animation complete and view is hidden */);
} else {
setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCurrentTargetOffsetTop); setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCurrentTargetOffsetTop);
}
mCurrentTargetOffsetTop = mRefreshView.getTop(); mCurrentTargetOffsetTop = mRefreshView.getTop();
} }
@ -355,7 +349,7 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
* @param progress * @param progress
*/ */
void setAnimationProgress(float progress) { void setAnimationProgress(float progress) {
mRefreshView.setProgressRotation(progress);
} }
private void setRefreshing(boolean refreshing, final boolean notify) { private void setRefreshing(boolean refreshing, final boolean notify) {
@ -731,14 +725,9 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
if (mRefreshView.getVisibility() != View.VISIBLE) { if (mRefreshView.getVisibility() != View.VISIBLE) {
mRefreshView.setVisibility(View.VISIBLE); mRefreshView.setVisibility(View.VISIBLE);
} }
if (!mScale) {
mRefreshView.setScaleX(1f); mRefreshView.setScaleX(1f);
mRefreshView.setScaleY(1f); mRefreshView.setScaleY(1f);
}
if (mScale) {
setAnimationProgress(Math.min(1f, overscrollTop / mTotalDragDistance));
}
setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop); setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop);
} }
@ -749,7 +738,6 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
// cancel refresh // cancel refresh
mRefreshing = false; mRefreshing = false;
Animation.AnimationListener listener = null; Animation.AnimationListener listener = null;
if (!mScale) {
listener = new Animation.AnimationListener() { listener = new Animation.AnimationListener() {
@Override @Override
@ -758,17 +746,14 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
@Override @Override
public void onAnimationEnd(Animation animation) { public void onAnimationEnd(Animation animation) {
if (!mScale) {
startScaleDownAnimation(null); startScaleDownAnimation(null);
} }
}
@Override @Override
public void onAnimationRepeat(Animation animation) { public void onAnimationRepeat(Animation animation) {
} }
}; };
}
animateOffsetToStartPosition(mCurrentTargetOffsetTop, listener); animateOffsetToStartPosition(mCurrentTargetOffsetTop, listener);
} }
} }
@ -873,10 +858,6 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
} }
private void animateOffsetToStartPosition(int from, AnimationListener listener) { private void animateOffsetToStartPosition(int from, AnimationListener listener) {
if (mScale) {
// Scale the item back down
startScaleDownReturnToStartAnimation(from, listener);
} else {
mFrom = from; mFrom = from;
mAnimateToStartPosition.reset(); mAnimateToStartPosition.reset();
mAnimateToStartPosition.setDuration(ANIMATE_TO_START_DURATION); mAnimateToStartPosition.setDuration(ANIMATE_TO_START_DURATION);
@ -887,7 +868,6 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
mRefreshView.clearAnimation(); mRefreshView.clearAnimation();
mRefreshView.startAnimation(mAnimateToStartPosition); mRefreshView.startAnimation(mAnimateToStartPosition);
} }
}
private final Animation mAnimateToCorrectPosition = new Animation() { private final Animation mAnimateToCorrectPosition = new Animation() {
@Override @Override
@ -919,30 +899,14 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
} }
}; };
private void startScaleDownReturnToStartAnimation(int from,
Animation.AnimationListener listener) {
mFrom = from;
mStartingScale = mRefreshView.getScaleX();
mScaleDownToStartAnimation = new Animation() {
@Override
public void applyTransformation(float interpolatedTime, Transformation t) {
float targetScale = (mStartingScale + (-mStartingScale * interpolatedTime));
setAnimationProgress(targetScale);
moveToStart(interpolatedTime);
}
};
mScaleDownToStartAnimation.setDuration(SCALE_DOWN_DURATION);
if (listener != null) {
mRefreshView.setAnimationListener(listener);
}
mRefreshView.clearAnimation();
mRefreshView.startAnimation(mScaleDownToStartAnimation);
}
void setTargetOffsetTopAndBottom(int offset) { void setTargetOffsetTopAndBottom(int offset) {
mRefreshView.bringToFront(); mRefreshView.bringToFront();
ViewCompat.offsetTopAndBottom(mRefreshView, offset); ViewCompat.offsetTopAndBottom(mRefreshView, offset);
mCurrentTargetOffsetTop = mRefreshView.getTop(); mCurrentTargetOffsetTop = mRefreshView.getTop();
if (mRefreshView.getMeasuredHeight() > 0) {
mRefreshView.setProgressRotation((float) mRefreshView.getBottom() / (float) mRefreshView.getMeasuredHeight());
}
} }
private void onSecondaryPointerUp(MotionEvent ev) { private void onSecondaryPointerUp(MotionEvent ev) {