Skip to content

Commit d32b648

Browse files
committed
根据view的相对位移,来动态调整reset keep view 动画执行时间,提升体验
1 parent 737a9c5 commit d32b648

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

library/src/main/java/com/think/tlr/TLRCalculator.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ private void initAttrs(Context context, AttributeSet attrs) {
115115
} else if (index == R.styleable.TLRLinearLayout_keepFootLoading) {
116116
isKeepFootLoading = array.getBoolean(index, isKeepFootLoading);
117117
} else if (index == R.styleable.TLRLinearLayout_refreshMaxMoveDistance) {
118-
mRefreshMaxMoveDistance = array.getDimensionPixelOffset(index, mRefreshMaxMoveDistance);
118+
mRefreshMaxMoveDistance = array.getDimensionPixelOffset(index,
119+
mRefreshMaxMoveDistance);
119120
} else if (index == R.styleable.TLRLinearLayout_loadMaxMoveDistance) {
120-
mLoadMaxMoveDistance = array.getDimensionPixelOffset(index, mLoadMaxMoveDistance);
121+
mLoadMaxMoveDistance = array.getDimensionPixelOffset(index,
122+
mLoadMaxMoveDistance);
121123
}
122124
}
123125
} finally {
@@ -182,8 +184,6 @@ public void eventUp(float x, float y) {
182184

183185
/**
184186
* eventMove distance must more than {@link ViewConfiguration#getScaledTouchSlop()}
185-
*
186-
* @return
187187
*/
188188
public boolean canCalculatorV() {
189189
if (mDirection == Direction.DOWN || mDirection == Direction.UP) {
@@ -202,8 +202,6 @@ public void touchMoveLayoutView(int offsetY) {
202202

203203
/**
204204
* call view {@link android.view.View#offsetTopAndBottom(int)} method must cast offset to int
205-
*
206-
* @return
207205
*/
208206
private void moveOffsetY(int y) {
209207
if (y == 0) {
@@ -250,12 +248,14 @@ private int calculateMaxMoveDistance(int y, int totalOffsetY) {
250248
int tempTotalOffsetY = totalOffsetY + y;
251249

252250
// calculate refresh over max move distance
253-
if (tempTotalOffsetY > 0 && mRefreshMaxMoveDistance > 0 && tempTotalOffsetY > mRefreshMaxMoveDistance) {
251+
if (tempTotalOffsetY > 0 && mRefreshMaxMoveDistance > 0
252+
&& tempTotalOffsetY > mRefreshMaxMoveDistance) {
254253
y = mRefreshMaxMoveDistance - mTotalOffsetY;
255254
}
256255

257256
// calculate load over max move distance
258-
if (tempTotalOffsetY < 0 && mLoadMaxMoveDistance > 0 && -tempTotalOffsetY > mLoadMaxMoveDistance) {
257+
if (tempTotalOffsetY < 0 && mLoadMaxMoveDistance > 0
258+
&& -tempTotalOffsetY > mLoadMaxMoveDistance) {
259259
y = -mLoadMaxMoveDistance - mTotalOffsetY;
260260
}
261261
return y;
@@ -271,7 +271,8 @@ private void notifyPixOffset(int totalOffsetY, int height, int y) {
271271
offset = (float) (Math.round(((float) totalThresholdY / height) * 100)) / 100;
272272
}
273273
boolean isRefresh = totalOffsetY != 0 ? totalOffsetY > 0 : y < 0;
274-
mTLRUiHandler.onOffsetChanged(tLRLinearLayout.getTouchView(), isRefresh, totalOffsetY, totalThresholdY, y, offset);
274+
mTLRUiHandler.onOffsetChanged(tLRLinearLayout.getTouchView(), isRefresh, totalOffsetY,
275+
totalThresholdY, y, offset);
275276
}
276277

277278
private void setDirection(float xDiff, float yDiff) {
@@ -299,18 +300,21 @@ public void startAutoRefresh() {
299300
TLRLog.d("autoRefresh mHeadHeight:" + mHeadHeight);
300301
mStatusController.setAutoRefreshing(true);
301302
if (mHeadHeight == 0) {
302-
tLRLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
303-
@Override
304-
public void onGlobalLayout() {
305-
TLRLog.d("autoRefresh onGlobalLayout mHeadHeight:" + mHeadHeight);
306-
startAutoRefreshAnimator();
307-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
308-
tLRLinearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
309-
} else {
310-
tLRLinearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
311-
}
312-
}
313-
});
303+
tLRLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(
304+
new ViewTreeObserver.OnGlobalLayoutListener() {
305+
@Override
306+
public void onGlobalLayout() {
307+
TLRLog.d("autoRefresh onGlobalLayout mHeadHeight:" + mHeadHeight);
308+
startAutoRefreshAnimator();
309+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
310+
tLRLinearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(
311+
this);
312+
} else {
313+
tLRLinearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(
314+
this);
315+
}
316+
}
317+
});
314318
} else {
315319
startAutoRefreshAnimator();
316320
}
@@ -328,7 +332,8 @@ private void startAutoRefreshAnimator() {
328332
@Override
329333
public void onAnimationEnd(Animator animation) {
330334
mAutoAnimator.removeListener(this);
331-
TLRLog.v("startAutoRefreshAnimator isKeepHeadRefreshing:" + isKeepHeadRefreshing);
335+
TLRLog.v("startAutoRefreshAnimator isKeepHeadRefreshing:"
336+
+ isKeepHeadRefreshing);
332337
if (isKeepHeadRefreshing) {
333338
startKeepAnimator();
334339
} else {
@@ -379,7 +384,9 @@ private void startResetAnimator() {
379384
endAllRunningAnimator();
380385
int startY = mTotalOffsetY;
381386
mResetAnimator = ValueAnimator.ofInt(startY, 0);
382-
mResetAnimator.setDuration(mCloseAnimDuration);
387+
long duration = (long) (mCloseAnimDuration * ((float) Math.abs(mTotalOffsetY)
388+
/ mHeadHeight));
389+
mResetAnimator.setDuration(duration);
383390
mResetAnimator.setInterpolator(new DecelerateInterpolator());
384391
mResetAnimator.addUpdateListener(new AnimUpdateListener(startY));
385392
mResetAnimator.start();

0 commit comments

Comments
 (0)