MyReboundScrollView.java 源代码
package custom;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;
public class MyReboundScrollView extends ScrollView {
private static final int ANIM_TIME = 200;
private static final float MOVE_FACTOR = 0.5f;
private static final String TAG = "ElasticScrollView";
private boolean canPullDown;
private boolean canPullUp;
private View contentView;
private boolean enableScrolling;
public boolean isInterdictMoved;
private boolean isMoved;
private Rect originalRect;
private float startY;
public boolean isEnableScrolling() {
return this.enableScrolling;
}
public void setEnableScrolling(boolean z) {
this.enableScrolling = z;
}
public MyReboundScrollView(Context context) {
super(context);
this.originalRect = new Rect();
this.canPullDown = false;
this.canPullUp = false;
this.isMoved = false;
this.isInterdictMoved = false;
this.enableScrolling = true;
}
public MyReboundScrollView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
this.originalRect = new Rect();
this.canPullDown = false;
this.canPullUp = false;
this.isMoved = false;
this.isInterdictMoved = false;
this.enableScrolling = true;
}
@Override
protected void onFinishInflate() {
if (getChildCount() > 0) {
this.contentView = getChildAt(0);
}
}
@Override
protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
super.onLayout(z, i, i2, i3, i4);
View view = this.contentView;
if (view == null) {
return;
}
this.originalRect.set(view.getLeft(), this.contentView.getTop(), this.contentView.getRight(), this.contentView.getBottom());
}
@Override
public boolean dispatchTouchEvent(MotionEvent motionEvent) {
boolean z;
Log.e("setTopbarClickListener", "dispatchTouchEvent MyReboundScrollView" + this.isInterdictMoved + "::::" + this.enableScrolling);
if (this.isInterdictMoved) {
return super.dispatchTouchEvent(motionEvent);
}
if (this.contentView == null) {
return super.dispatchTouchEvent(motionEvent);
}
int action = motionEvent.getAction();
if (action != 0) {
boolean z2 = false;
if (action != 1) {
if (action == 2) {
if (!this.canPullDown && !this.canPullUp) {
this.startY = motionEvent.getY();
this.canPullDown = isCanPullDown();
this.canPullUp = isCanPullUp();
} else {
int y = (int) (motionEvent.getY() - this.startY);
boolean z3 = this.canPullDown;
if ((z3 && y > 0) || (((z = this.canPullUp) && y < 0) || (z && z3))) {
z2 = true;
}
if (z2) {
int i = (int) (y * 0.5f);
this.contentView.layout(this.originalRect.left, this.originalRect.top + i, this.originalRect.right, this.originalRect.bottom + i);
this.isMoved = true;
}
}
}
} else if (this.isMoved) {
TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 0.0f, this.contentView.getTop(), this.originalRect.top);
translateAnimation.setDuration(200L);
this.contentView.startAnimation(translateAnimation);
this.contentView.layout(this.originalRect.left, this.originalRect.top, this.originalRect.right, this.originalRect.bottom);
this.canPullDown = false;
this.canPullUp = false;
this.isMoved = false;
}
} else {
this.canPullDown = isCanPullDown();
this.canPullUp = isCanPullUp();
this.startY = motionEvent.getY();
}
return super.dispatchTouchEvent(motionEvent);
}
private boolean isCanPullDown() {
return getScrollY() == 0 || this.contentView.getHeight() < getHeight() + getScrollY();
}
private boolean isCanPullUp() {
return this.contentView.getHeight() <= getHeight() + getScrollY();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
Log.e("setTopbarClickListener", "onInterceptTouchEvent MyReboundScrollView" + this.isInterdictMoved + "::::" + this.enableScrolling);
if (isEnableScrolling()) {
return super.onInterceptTouchEvent(motionEvent);
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
Log.e("setTopbarClickListener", "onTouchEvent MyReboundScrollView" + this.isInterdictMoved + "::::" + this.enableScrolling);
if (isEnableScrolling()) {
return super.onTouchEvent(motionEvent);
}
return false;
}
}