弹性ScrollView(备忘)

备忘,加深记忆

    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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值