【Android开发】 View的滑动

View的滑动

View 的滑动式 Android 实现自定义控件的基础。可以通过以下几种方式实现 View 的滑动。

layout()

View 在进行绘制的时候会调用 onLayout() 方法来设置显示的位置。通过修改 Viewlefttoprightbottom 这4个属性来控制 View 的坐标。

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 获取手指触摸点的横坐标和纵坐标
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                performClick();
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                // 计算移动的距离
                int offsetX = x - lastX;
                int offsetY = y - lastY;
                // 调用layout方法来重新放置它的位置
                layout(getLeft() + offsetX, getTop() + offsetY,
                        getRight() + offsetX, getBottom() + offsetY);
                break;
        }
        return true;
    }

注意:自定义空间重写 onTouchEvent() 必须同时重写 performClick()

offsetLeftAndRight()与offsetTopAndBottom()

		// 计算移动的距离
        int offsetX = x - lastX;
        int offsetY = y - lastY;
        // 对left和right进行偏移
        offsetLeftAndRight(offsetX);
        // 对top和bottom进行偏移
        offsetTopAndBottom(offsetY);

LayoutParams

        // 计算移动的距离
        int offsetX = x - lastX;
        int offsetY = y - lastY;
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
        layoutParams.leftMargin = getLeft() + offsetX;
        layoutParams.topMargin = getTop() + offsetY;
        setLayoutParams(layoutParams);

scrollTo与scrollBy

scrollToscrollBy 移动 View 的内容,具体源码如下:

    /**
     * Set the scrolled position of your view. This will cause a call to
     * {@link #onScrollChanged(int, int, int, int)} and the view will be
     * invalidated.
     * @param x the x position to scroll to
     * @param y the y position to scroll to
     */
    public void scrollTo(int x, int y) {
        if (mScrollX != x || mScrollY != y) {
            int oldX = mScrollX;
            int oldY = mScrollY;
            mScrollX = x;
            mScrollY = y;
            invalidateParentCaches();
            onScrollChanged(mScrollX, mScrollY, oldX, oldY);
            if (!awakenScrollBars()) {
                postInvalidateOnAnimation();
            }
        }
    }

    /**
     * Move the scrolled position of your view. This will cause a call to
     * {@link #onScrollChanged(int, int, int, int)} and the view will be
     * invalidated.
     * @param x the amount of pixels to scroll by horizontally
     * @param y the amount of pixels to scroll by vertically
     */
    public void scrollBy(int x, int y) {
        scrollTo(mScrollX + x, mScrollY + y);
    }

注意:如果要移动的是整个ViewGroup,要使用以下代码

((View)getParent()).scrollBy(-offsetX, -offsetY);

Scroller

可以使用 Scroller 来实现有过渡效果的滑动,不是瞬间完成的,而是在一定的时间间隔内完成的。Scroller 本身不能实现 View 的滑动的,它需要与 ViewcomputeScroll() 方法配合才能实现弹性滑动的效果。

    public TestView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        scroller = new Scroller(context);
    }

接下来重写 computeScroll() 方法,系统会在绘制 View 的时候在 draw() 方法中调用该方法。在这个方法里,我们调用父类的 scrollTo() 方法并通过 Scroller 来不断获取当前的滚动值,每次滑动就调用 invalidate() 方法不断进行重绘,重绘就调用 computeScroll() 方法。

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()) {
            ((View) getParent()).scrollTo(scroller.getCurrX(), scroller.getCurrY());
            invalidate();
        }
    }

接下来调用 ScrollerstartScroll() 方法即可实现平移。

    public void smoothScrollTo(int destX, int destY) {
        int scrollX = getScrollX();
        int deltaX = destX - scrollX;
        scroller.startScroll(scrollX, 0, deltaX, 0, 2000);
        invalidate();
    }

《Android进阶之光》刘望舒 电子工业出版社

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hovf-1120

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值