备忘,加深记忆
publivc class ReboundScrollView extends ScrollView{
private isTop=falsle;
private isMoved=false;
private inr startY;
private Retc retc=new Retc();
private View contentView;
public ReboundScrollView(Context context) {
super(context);
}
public ReboundScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onFinishInFilter(){
if(getChildCount>0){
contentView=getChildAt(0);
}
}
@Override
public void onLayout(){
if(contentView!=null){
rect.set(contentView.getLeft(),contentView.getTop(),
contentView.getRight(),contentView.getBottom);
}
}
@Override
pubic boolean dispatchTouchEvent(MotionEvent ev){
if(contentView==null) return super.dispatchTouchEvnet(ev);
int action =ev.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
startY=(int)ev.getY();
isTop=getIsTop();
isBottom=getIsBottom();
break;
case MotionEvnet.ACTION_UP:
if(!isMoved) break;
TranslateAnimation anim=new TranslateAnimation(0,0,
contentView.getTop(),retc.top);
contentView.setDuration(300);
contentView.startAnimation(anim);
contentView.layout(retc.left,retc.top,retc.left,retc.botttom);
isMoved=false;
isTop=false;
isBottom=false;
break;
case MotionEvent.ACTION_MOVE:
if(!isTop&&!isBottom){
startY=ev.getY();
isTop=getIsTop();
isBottom=getIsBottom();
break;
}
int dy=(int)(ev.getY()-startY)*0.5;
boolean shouldMove= isTop&&dy>0||isBottom&&dy<0||isTop&&isBottom;
if(shouldMove){
contentView.layout(rect.getLeft(),rect.getTop()+dy,
rect.getRight(),rect.getBottom()+dy);
isMoved=true;
}
break;
}
}
private boolean GetIsTop(){
return getScrollY()=0||content.getHeight()<getHeight+getScrollY();
}
private boolean GetIsBottom(){
return content.getHeight()<=getHeight+getScrollY();
}
}