上下面板切换效果(类似大黑板效果)

上篇blog介绍了ViewDragHelper的简单的使用,这篇blog再用ViewDragHelper来实现一个效果,先上图
图1
再上代码:

public class BlackboardView extends ViewGroup {

    private int mWidth;//控件的宽
    private int mHeight;//控件的高
    private View topView, bottonView, capturedView;
    private ViewDragHelper mHelper;
    private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return (topView == child) || (bottonView == child);
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            if (top < 0)
                return 0;
            if (top > mHeight / 2)
                return mHeight / 2;
            return top;
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            return 0;
        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
            if (releasedChild.getTop() < mHeight / 4)
                mHelper.smoothSlideViewTo(releasedChild, 0, 0);
            else
                mHelper.smoothSlideViewTo(releasedChild, 0, mHeight / 2);
            ViewCompat.postInvalidateOnAnimation(BlackboardView.this);
        }

        @Override
        public void onViewCaptured(View capturedChild, int activePointerId) {
            super.onViewCaptured(capturedChild, activePointerId);
            capturedView = capturedChild;
        }

        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            super.onViewPositionChanged(changedView, left, top, dx, dy);
            if (changedView == capturedView) {
                if (changedView == topView)
                    bottonView.offsetTopAndBottom(-dy);
                else if (changedView == bottonView)
                    topView.offsetTopAndBottom(-dy);
            }
        }
    };

    public BlackboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BlackboardView(Context context) {
        super(context);
        init();
    }

    public BlackboardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mHelper = ViewDragHelper.create(this, callback);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        for (int i = 0; i < getChildCount(); i++) {
            measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        topView = getChildAt(0);
        bottonView = getChildAt(1);
        topView.layout(0, 0, mWidth, mHeight / 2);
        bottonView.layout(0, mHeight / 2, mWidth, mHeight);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mHelper.processTouchEvent(event);
        return true;
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

代码不难,比起上一篇blog,只是callback多2个回调函数,以及控制了两个子view的滑动。
public void onViewCaptured(View capturedChild, int activePointerId)
当滑动某一个子view时回调,down-move-up过程中只回调一次
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy)
当子view位置变化时调用,不一定是拖动的那个。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值