Skip to content

Commit

Permalink
Fixed the bad performance due to paddings and margins.
Browse files Browse the repository at this point in the history
  • Loading branch information
woxingxiao committed Sep 28, 2018
1 parent 2cc372d commit 761d931
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 2
versionName "1.1"
versionCode 3
versionName "1.2"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
67 changes: 42 additions & 25 deletions library/src/main/java/com/xw/repo/widget/BounceScrollView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.MarginLayoutParamsCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.view.animation.TranslateAnimation;

Expand All @@ -20,7 +22,7 @@ public class BounceScrollView extends NestedScrollView {
private static final int DEFAULT_SCROLL_THRESHOLD = 20;
private static final long DEFAULT_BOUNCE_DELAY = 400;

private boolean mHorizontal;
private boolean isHorizontal;
private float mDamping;
private boolean mIncrementalDamping;
private long mBounceDelay;
Expand Down Expand Up @@ -54,7 +56,7 @@ public BounceScrollView(@NonNull Context context, @Nullable AttributeSet attrs,
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BounceScrollView, 0, 0);
mDamping = a.getFloat(R.styleable.BounceScrollView_damping, DEFAULT_DAMPING_COEFFICIENT);
int orientation = a.getInt(R.styleable.BounceScrollView_scrollOrientation, 0);
mHorizontal = orientation == 1;
isHorizontal = orientation == 1;
mIncrementalDamping = a.getBoolean(R.styleable.BounceScrollView_incrementalDamping, true);
mBounceDelay = a.getInt(R.styleable.BounceScrollView_bounceDelay, (int) DEFAULT_BOUNCE_DELAY);
mTriggerOverScrollThreshold = a.getInt(R.styleable.BounceScrollView_triggerOverScrollThreshold, DEFAULT_SCROLL_THRESHOLD);
Expand All @@ -76,23 +78,23 @@ protected void onFinishInflate() {

@Override
public boolean canScrollVertically(int direction) {
return !mHorizontal;
return !isHorizontal;
}

@Override
public boolean canScrollHorizontally(int direction) {
return mHorizontal;
return isHorizontal;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mStart = mHorizontal ? ev.getX() : ev.getY();
mStart = isHorizontal ? ev.getX() : ev.getY();

break;
case MotionEvent.ACTION_MOVE:
if (mHorizontal) {
if (isHorizontal) {
float scrollX = ev.getX() - mStart;
return Math.abs(scrollX) >= mTriggerOverScrollThreshold;
} else {
Expand Down Expand Up @@ -123,7 +125,7 @@ public boolean onTouchEvent(MotionEvent ev) {
float now, delta;
int dampingDelta;

now = mHorizontal ? ev.getX() : ev.getY();
now = isHorizontal ? ev.getX() : ev.getY();
delta = mStart - now;
dampingDelta = (int) (delta / calculateDamping());
mStart = now;
Expand All @@ -142,7 +144,7 @@ public boolean onTouchEvent(MotionEvent ev) {
mChildView.getRight(), mChildView.getBottom());
}

if (mHorizontal) {
if (isHorizontal) {
mChildView.layout(mChildView.getLeft() - dampingDelta, mChildView.getTop(),
mChildView.getRight() - dampingDelta, mChildView.getBottom());
} else {
Expand Down Expand Up @@ -173,7 +175,7 @@ public boolean onTouchEvent(MotionEvent ev) {

private float calculateDamping() {
float ratio;
if (mHorizontal) {
if (isHorizontal) {
ratio = Math.abs(mChildView.getLeft()) * 1.0f / mChildView.getMeasuredWidth();
} else {
ratio = Math.abs(mChildView.getTop()) * 1.0f / mChildView.getMeasuredHeight();
Expand All @@ -189,22 +191,37 @@ private float calculateDamping() {

private void resetChildViewWithAnimation() {
TranslateAnimation anim;
if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL) {
if (mHorizontal) {
anim = new TranslateAnimation(mChildView.getPaddingRight(), mNormalRect.right - getPaddingRight(),
0, 0);

ViewGroup.LayoutParams layoutParams = mChildView.getLayoutParams();
int fixedPadding;
int fixedMargin = 0;
if (isHorizontal) {
if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL) {
fixedPadding = ViewCompat.getPaddingEnd(this);
if (layoutParams != null && layoutParams instanceof MarginLayoutParams) {
fixedMargin = MarginLayoutParamsCompat.getMarginEnd((MarginLayoutParams) layoutParams);
}
} else {
anim = new TranslateAnimation(0, 0, mChildView.getTop(),
mNormalRect.top - getPaddingTop());
fixedPadding = ViewCompat.getPaddingStart(this);
if (layoutParams != null && layoutParams instanceof MarginLayoutParams) {
fixedMargin = MarginLayoutParamsCompat.getMarginStart((MarginLayoutParams) layoutParams);
}
}
anim = new TranslateAnimation(
mChildView.getLeft() - fixedPadding - fixedMargin,
mNormalRect.left - fixedPadding - fixedMargin,
0,
0);
} else {
if (mHorizontal) {
anim = new TranslateAnimation(mChildView.getLeft(), mNormalRect.left - getPaddingLeft(),
0, 0);
} else {
anim = new TranslateAnimation(0, 0, mChildView.getTop(),
mNormalRect.top - getPaddingTop());
fixedPadding = getPaddingTop();
if (layoutParams != null && layoutParams instanceof MarginLayoutParams) {
fixedMargin = ((MarginLayoutParams) layoutParams).topMargin;
}
anim = new TranslateAnimation(
0,
0,
mChildView.getTop() - fixedPadding - fixedMargin,
mNormalRect.top - fixedPadding - fixedMargin);
}
anim.setInterpolator(mInterpolator);
anim.setDuration(mBounceDelay);
Expand All @@ -219,11 +236,11 @@ private boolean canMove(int delta) {
}

private boolean canMoveFromStart() {
return mHorizontal ? getScrollX() == 0 : getScrollY() == 0;
return isHorizontal ? getScrollX() == 0 : getScrollY() == 0;
}

private boolean canMoveFromEnd() {
if (mHorizontal) {
if (isHorizontal) {
int offset = mChildView.getMeasuredWidth() - getWidth();
offset = offset < 0 ? 0 : offset;
return getScrollX() == offset;
Expand All @@ -244,11 +261,11 @@ protected void onScrollChanged(int scrollX, int scrollY, int oldl, int oldt) {
}

public void setScrollHorizontally(boolean horizontal) {
this.mHorizontal = horizontal;
this.isHorizontal = horizontal;
}

public boolean isScrollHorizontally() {
return mHorizontal;
return isHorizontal;
}

public float getDamping() {
Expand Down

0 comments on commit 761d931

Please sign in to comment.