ViewDragHelper让你轻松让View动起来

ViewDragHelper 是V4包提供的View拖拽辅助类,用它可以很方便的处理View拖拽,比如探探的卡片功能。

1.创建一个ViewGroup控件:

public class SlideView extends FrameLayout {


    public SlideView(@NonNull Context context) {
        this(context, null);
    }

    public SlideView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }
}

2.创建ViewDragHelper对象:

  • ViewDragHelper构造:
static ViewDragHelper   create(ViewGroup forParent, float sensitivity, ViewDragHelper.Callback cb)

static ViewDragHelper   create(ViewGroup forParent, ViewDragHelper.Callback cb)
  • 通过静态方法创建对象:
private void initDragHelper() {
        dragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() {
            //默认重写的方法(返回值决定代表是否可以拖拽)
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                //返回true才能拖拽
                return true;
            }
            //手动重写方法(水平滑动需要重写此方法)
            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                return left;
            }
            //手动重写方法(垂直滑动需要重写此方法)
            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                return top;
            }
        });
    }

    //拦截事件交给ViewDragHelper处理
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return dragHelper.shouldInterceptTouchEvent(ev);
    }
    //触摸事件交给ViewDragHelper处理
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        dragHelper.processTouchEvent(event);
        return true;
    }

3.创建布局文件,引入我们写的SlideView:

    <viewdrag.chao.com.viewdraghelper.SlideView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="#50ff0000"
            android:gravity="center"
            android:text="我是文本"
            android:textSize="30sp"
            android:textStyle="bold" />

    </viewdrag.chao.com.viewdraghelper.SlideView>
  • 预览:
预览

4.限制:

  • 限制只能拖拽某个View:
 @Override
 public boolean tryCaptureView(View child, int pointerId) {
     return child==getChildAt(1);//只允许第二个View可以拖拽
 }
  • 边界限制:
   @Override
   public int clampViewPositionHorizontal(View child, int left, int dx) {
   //限制左边边界最低为0
        if (child.getLeft() + left <= 0) {
              return 0;
         }
     return left;
     }
  • 手势释放回调(重写ViewDragHelper回调的onViewReleased):
    @Override
    public void onViewReleased(View releasedChild, float xvel, float yvel) {
         super.onViewReleased(releasedChild, xvel, yvel);
         dragHelper.settleCapturedViewAt(0, 0);//释放后回到原点
         invalidate();
     }

然后重写View类的平滑滚动方法:

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (dragHelper != null && dragHelper.continueSettling(true)) {
            invalidate();
        }
    }

预览:

回弹

要做拖拽View是不是很简单呢?把图片放大,做一些边界控制加一些简单动画即可达到探探的卡片滑动效果。

由于代码不多,只写了个测试类,就不上传仓库了,下面贴出源码:

package viewdrag.chao.com.viewdraghelper;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

/**
 * Created by Chao on 2018-02-03.
 */

public class SlideView extends FrameLayout {
    private ViewDragHelper dragHelper;


    public SlideView(@NonNull Context context) {
        this(context, null);
    }

    public SlideView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDragHelper();
    }

    private void initDragHelper() {
        dragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                return true;
            }

            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                if (child.getLeft() + left <= 0) {
                    return 0;
                }
                return left;
            }

            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                return top;
            }

            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                super.onViewReleased(releasedChild, xvel, yvel);
                dragHelper.settleCapturedViewAt(0, 0);
                invalidate();
            }
        });
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (dragHelper != null && dragHelper.continueSettling(true)) {
            invalidate();
        }
    }

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

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

}

发表评论