feat:Optimize Android's swipelayout

This commit is contained in:
pengfei.zhou 2019-12-03 09:59:09 +08:00
parent 6d86b8cfeb
commit eefea8bc2a
2 changed files with 35 additions and 64 deletions

View File

@ -119,11 +119,6 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
int mCustomSlingshotDistance; int mCustomSlingshotDistance;
private Animation mScaleAnimation;
private Animation mScaleDownAnimation;
private Animation mScaleDownToStartAnimation;
boolean mNotify; boolean mNotify;
@ -133,6 +128,7 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
private OnChildScrollUpCallback mChildScrollUpCallback; private OnChildScrollUpCallback mChildScrollUpCallback;
private DoricRefreshView mRefreshView; private DoricRefreshView mRefreshView;
private AnimationListener mRefreshListener = new AnimationListener() { private AnimationListener mRefreshListener = new AnimationListener() {
@Override @Override
public void onAnimationStart(Animation animation) { public void onAnimationStart(Animation animation) {
@ -160,6 +156,20 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
private int mPullDownHeight = 0; private int mPullDownHeight = 0;
private ValueAnimator headerViewAnimator; private ValueAnimator headerViewAnimator;
private void onRefreshAnimationEnd() {
if (mRefreshing) {
mRefreshView.startAnimation();
if (mNotify) {
if (mListener != null) {
mListener.onRefresh();
}
}
mCurrentTargetOffsetTop = mRefreshView.getTop();
} else {
reset();
}
}
void reset() { void reset() {
mRefreshing = false; mRefreshing = false;
if (headerViewAnimator != null && headerViewAnimator.isRunning()) { if (headerViewAnimator != null && headerViewAnimator.isRunning()) {
@ -171,6 +181,9 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
public void onAnimationUpdate(ValueAnimator animation) { public void onAnimationUpdate(ValueAnimator animation) {
mCurrentTargetOffsetTop = (int) animation.getAnimatedValue() mCurrentTargetOffsetTop = (int) animation.getAnimatedValue()
- mRefreshView.getMeasuredHeight(); - mRefreshView.getMeasuredHeight();
if (mRefreshView.getMeasuredHeight() > 0) {
mRefreshView.setPullingDistance(DoricUtils.px2dp(mRefreshView.getBottom()));
}
mRefreshView.requestLayout(); mRefreshView.requestLayout();
} }
}); });
@ -322,36 +335,13 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
} }
setTargetOffsetTopAndBottom(endTarget - mCurrentTargetOffsetTop); setTargetOffsetTopAndBottom(endTarget - mCurrentTargetOffsetTop);
mNotify = false; mNotify = false;
startScaleUpAnimation(mRefreshListener); mRefreshView.setVisibility(View.VISIBLE);
onRefreshAnimationEnd();
} else { } else {
setRefreshing(refreshing, false /* notify */); setRefreshing(refreshing, false /* notify */);
} }
} }
private void startScaleUpAnimation(AnimationListener listener) {
mRefreshView.setVisibility(View.VISIBLE);
mScaleAnimation = new Animation() {
@Override
public void applyTransformation(float interpolatedTime, Transformation t) {
setAnimationProgress(interpolatedTime);
}
};
mScaleAnimation.setDuration(mMediumAnimationDuration);
if (listener != null) {
mRefreshView.setAnimationListener(listener);
}
mRefreshView.clearAnimation();
mRefreshView.startAnimation(mScaleAnimation);
}
/**
* Pre API 11, this does an alpha animation.
*
* @param progress
*/
void setAnimationProgress(float progress) {
}
private void setRefreshing(boolean refreshing, final boolean notify) { private void setRefreshing(boolean refreshing, final boolean notify) {
if (mRefreshing != refreshing) { if (mRefreshing != refreshing) {
@ -361,24 +351,11 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
if (mRefreshing) { if (mRefreshing) {
animateOffsetToCorrectPosition(mCurrentTargetOffsetTop, mRefreshListener); animateOffsetToCorrectPosition(mCurrentTargetOffsetTop, mRefreshListener);
} else { } else {
startScaleDownAnimation(mRefreshListener); onRefreshAnimationEnd();
} }
} }
} }
void startScaleDownAnimation(Animation.AnimationListener listener) {
mScaleDownAnimation = new Animation() {
@Override
public void applyTransformation(float interpolatedTime, Transformation t) {
setAnimationProgress(1 - interpolatedTime);
}
};
mScaleDownAnimation.setDuration(SCALE_DOWN_DURATION);
mRefreshView.setAnimationListener(listener);
mRefreshView.clearAnimation();
mRefreshView.startAnimation(mScaleDownAnimation);
}
/** /**
* @return Whether the SwipeRefreshWidget is actively showing refresh * @return Whether the SwipeRefreshWidget is actively showing refresh
* progress. * progress.
@ -746,24 +723,7 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
} else { } else {
// cancel refresh // cancel refresh
mRefreshing = false; mRefreshing = false;
Animation.AnimationListener listener = null; animateOffsetToStartPosition(mCurrentTargetOffsetTop, null);
listener = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
startScaleDownAnimation(null);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
};
animateOffsetToStartPosition(mCurrentTargetOffsetTop, listener);
} }
} }

View File

@ -17,6 +17,7 @@ function thisLabel(str: string) {
class AnimationDemo extends Panel { class AnimationDemo extends Panel {
build(rootView: Group): void { build(rootView: Group): void {
const view = box(2) const view = box(2)
const view2 = box(3)
view.onClick = () => { view.onClick = () => {
modal(context).toast('Clicked') modal(context).toast('Clicked')
} }
@ -177,7 +178,17 @@ class AnimationDemo extends Panel {
] ]
).apply({ space: 10 } as IVLayout), ).apply({ space: 10 } as IVLayout),
stack([ stack([
view, view.also(v => {
v.x = 20
v.y = 0
v.width = 30
v.left = 15
}),
view2.also(v => {
v.x = v.y = 20
v.y = 40
v.scaleX = 1.5
})
]).apply({ ]).apply({
layoutConfig: layoutConfig().atmost(), layoutConfig: layoutConfig().atmost(),
backgroundColor: colors[1].alpha(0.3 * 255), backgroundColor: colors[1].alpha(0.3 * 255),