弹性ScrollView(备忘)

来源:互联网 发布:淘宝培训课件 编辑:程序博客网 时间:2024/05/17 11:59

备忘,加深记忆

    publivc class ReboundScrollView extends ScrollView{        //是否在顶部        private isTop=falsle;        //是否在底部        private isMoved=false;        //按下时的y坐标        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(){        //scrollView内只有一个子view            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(){            // 距离顶部距离为0 或内容的高度小于scrollview的高度加距离顶部的距离,            //也就是contentview不能填满scrollview            return getScrollY()=0||content.getHeight()<getHeight+getScrollY();        }        private boolean GetIsBottom(){            return content.getHeight()<=getHeight+getScrollY();        }    }
0 0
原创粉丝点击