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;
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);
}
}

View File

@ -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),