android可滑动的View-成都快上网建站

android可滑动的View

   效果图如下:

我们提供的服务有:网站建设、成都网站建设、微信公众号开发、网站优化、网站认证、土默特左旗ssl等。为近千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的土默特左旗网站制作公司

android 可滑动的View

方法一(实现原理,主要依靠移动动画),主要代码如下:

private OnTouchListener gonggaoOnTouch() {
        return new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    if (mAdsV.getTop() == mSlipRl.getTop() && moveY < 0) {
                        topTag();
                    } else if (moveY < -200) {
                        topTag();
                    } else {
                        nextTag();
                    }
                    break;
                }
                }
                gestureDetector.onTouchEvent(event);
                return true;
            }
            private void topTag() {
                animBuilder.setDurationMillis(400).setFromYValue(formY)
                        .setToYValue(mAdsV.getTop() - mSlipRl.getTop());
                bgMoveAnim = animBuilder.build();
                bgMoveAnim.setAnimationListener(new AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        // TODO Auto-generated method stub
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        // TODO Auto-generated method stub
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        // TODO Auto-generated method stub
                        mSlipRl.clearAnimation();
                        setLayout(mSlipRl, 0, mAdsV.getTop(), 0);
                        mSlipRl.invalidate();
                    }
                });
                mSlipRl.startAnimation(bgMoveAnim);
            }
            private void nextTag() {
                animBuilder.setDurationMillis(400).setFromYValue(formY)
                        .setToYValue(mAdsV.getBottom() - mSlipRl.getTop());
                bgMoveAnim = animBuilder.build();
                bgMoveAnim.setAnimationListener(new AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        // TODO Auto-generated method stub
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        // TODO Auto-generated method stub
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        // TODO Auto-generated method stub
                        mSlipRl.clearAnimation();
                        setLayout(mSlipRl, 0, mAdsV.getBottom(), 0);
                        mSlipRl.invalidate();
                    }
                });
                mSlipRl.startAnimation(bgMoveAnim);
            }
        };
    }

方法二,主要代码如下:

@Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            lastY = event.getY();
            if(null==mLocationSlip||null==mSlipDown||null==mSlipMove){
                mLocationSlip = this.findViewById(R.id.location_slip);
                mSlipDown = this.findViewById(R.id.down_slip);
                mSlipMove = this.findViewById(R.id.move_slip);
            }
            top = mSlipMove.getTop();
            bottom = mSlipDown.getBottom();
            if (top <= lastY && lastY <= (bottom + top)) {
                isMove = true;
                return true;
            }
        case MotionEvent.ACTION_MOVE:
            if (isMove) {
                float newY = event.getY();
                int ay = (int) ((lastY - newY) * 0.5);
                lastY = newY;
                mSlipMove.offsetTopAndBottom(-ay);
            }
            break;
        case MotionEvent.ACTION_UP:
            if (isMove) {
                isMove = false;
                top = mSlipMove.getTop();
                System.out.println(top + "====" + bottom + "==="+ mLocationSlip.getBottom()*2/3);
                if(!location&&top<(mLocationSlip.getBottom()*2/3)){
                    setLayout(mSlipMove, 0, 0, 0);
                    location=true;
                }else if(location&&top>(mLocationSlip.getBottom()*1/3)){
                    setLayout(mSlipMove, 0, mLocationSlip.getBottom(), 0);
                    location=false;
                }else if(!location){
                    setLayout(mSlipMove, 0, mLocationSlip.getBottom(), 0);
                }else{
                    setLayout(mSlipMove, 0, 0, 0);
                }
            }
            break;
        }
        return super.dispatchTouchEvent(event);
    }

主要知识点,就是重新设置了View的位置,android 3.0之后View可以同setTop(),setLeft()等一系列方法直接设置位置方便了开发。3.0之前位置变化设置方法:

/**
     * 给view重置位置
     * @param view
     * @param x
     * @param y
     * @param b
     */
    public void setLayout(View view, int x, int y, int b) {
        MarginLayoutParams margin = new MarginLayoutParams(
                view.getLayoutParams());
        margin.setMargins(x, y, 0, b);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                margin);
        view.setLayoutParams(layoutParams);
    }

文章涉及知识点不多,相信一看就懂。如有问题,留言

下载移步:可滑动的View


本文标题:android可滑动的View
当前地址:http://kswjz.com/article/gpdjhi.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流