From eefea8bc2a0b8580797c0622c15c7f27deccbcdc Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Tue, 3 Dec 2019 09:59:09 +0800 Subject: [PATCH] feat:Optimize Android's swipelayout --- .../pub/doric/refresh/DoricSwipeLayout.java | 84 +++++-------------- demo/src/ComplicatedAnimations.ts | 15 +++- 2 files changed, 35 insertions(+), 64 deletions(-) diff --git a/Android/doric/src/main/java/pub/doric/refresh/DoricSwipeLayout.java b/Android/doric/src/main/java/pub/doric/refresh/DoricSwipeLayout.java index fec95651..fca764d5 100644 --- a/Android/doric/src/main/java/pub/doric/refresh/DoricSwipeLayout.java +++ b/Android/doric/src/main/java/pub/doric/refresh/DoricSwipeLayout.java @@ -119,11 +119,6 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent int mCustomSlingshotDistance; - private Animation mScaleAnimation; - - private Animation mScaleDownAnimation; - - private Animation mScaleDownToStartAnimation; boolean mNotify; @@ -133,6 +128,7 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent private OnChildScrollUpCallback mChildScrollUpCallback; private DoricRefreshView mRefreshView; + private AnimationListener mRefreshListener = new AnimationListener() { @Override public void onAnimationStart(Animation animation) { @@ -160,6 +156,20 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent private int mPullDownHeight = 0; private ValueAnimator headerViewAnimator; + private void onRefreshAnimationEnd() { + if (mRefreshing) { + mRefreshView.startAnimation(); + if (mNotify) { + if (mListener != null) { + mListener.onRefresh(); + } + } + mCurrentTargetOffsetTop = mRefreshView.getTop(); + } else { + reset(); + } + } + void reset() { mRefreshing = false; if (headerViewAnimator != null && headerViewAnimator.isRunning()) { @@ -171,6 +181,9 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent public void onAnimationUpdate(ValueAnimator animation) { mCurrentTargetOffsetTop = (int) animation.getAnimatedValue() - mRefreshView.getMeasuredHeight(); + if (mRefreshView.getMeasuredHeight() > 0) { + mRefreshView.setPullingDistance(DoricUtils.px2dp(mRefreshView.getBottom())); + } mRefreshView.requestLayout(); } }); @@ -322,36 +335,13 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent } setTargetOffsetTopAndBottom(endTarget - mCurrentTargetOffsetTop); mNotify = false; - startScaleUpAnimation(mRefreshListener); + mRefreshView.setVisibility(View.VISIBLE); + onRefreshAnimationEnd(); } else { 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) { if (mRefreshing != refreshing) { @@ -361,24 +351,11 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent if (mRefreshing) { animateOffsetToCorrectPosition(mCurrentTargetOffsetTop, mRefreshListener); } 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 * progress. @@ -746,24 +723,7 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent } else { // cancel refresh mRefreshing = false; - Animation.AnimationListener listener = 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); + animateOffsetToStartPosition(mCurrentTargetOffsetTop, null); } } diff --git a/demo/src/ComplicatedAnimations.ts b/demo/src/ComplicatedAnimations.ts index 75cdca97..bc477830 100644 --- a/demo/src/ComplicatedAnimations.ts +++ b/demo/src/ComplicatedAnimations.ts @@ -17,11 +17,12 @@ function thisLabel(str: string) { class AnimationDemo extends Panel { build(rootView: Group): void { const view = box(2) + const view2 = box(3) view.onClick = () => { modal(context).toast('Clicked') } vlayout([ - title("Complicated Animation"), + title("Complicated Animation"), vlayout( [ hlayout([ @@ -177,7 +178,17 @@ class AnimationDemo extends Panel { ] ).apply({ space: 10 } as IVLayout), 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({ layoutConfig: layoutConfig().atmost(), backgroundColor: colors[1].alpha(0.3 * 255),