自定义拖拽效果

拖拽效果

如图,要想实现上图的效果,需要自定义ViewGroup,自定义ViewGroup的代码如下:


public class DragSlideLayout extends RelativeLayout {

    private View mDragView;
    private ViewDragHelper mDragHelper;

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

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

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

    private void init() {
        mDragHelper = ViewDragHelper.create(this, 1.0f, new DragHelperCallback());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int childCount = getChildCount();
        mDragView = getChildAt(childCount - 1);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = MotionEventCompat.getActionMasked(ev);
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            mDragHelper.cancel();
            return false;
        }
        return mDragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        mDragHelper.processTouchEvent(ev);
        return true;
    }

    class DragHelperCallback extends ViewDragHelper.Callback {

        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return child == mDragView;
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            final int topBound = getPaddingTop();
            final int bottomBound = getHeight() - mDragView.getHeight() - getPaddingBottom();
            return Math.min(Math.max(top, topBound), bottomBound);
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            final int leftBound = getPaddingLeft();
            final int rightBound = getWidth() - mDragView.getWidth() - getPaddingRight();
            return Math.min(Math.max(left, leftBound), rightBound);
        }

        @Override
        public int getViewVerticalDragRange(View child) {
            return getMeasuredHeight() - child.getMeasuredHeight();
        }

        @Override
        public int getViewHorizontalDragRange(View child) {
            return getMeasuredWidth() - child.getMeasuredWidth() - getPaddingRight();
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
            int leftBound = getPaddingLeft();
            int rightBound = getViewHorizontalDragRange(releasedChild);
            int toTarget = releasedChild.getX() > (getMeasuredWidth() / 2 - releasedChild.getMeasuredWidth() / 2) ? rightBound : leftBound;
            mDragHelper.settleCapturedViewAt(toTarget, (int) releasedChild.getY());
            invalidate();
        }
    }

    @Override
    public void computeScroll() {
        if (mDragHelper.continueSettling(true)) {
            invalidate();
        }
    }
}

其中xml的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<com.example.itservice.myapplication.DragSlideLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/iv_store_guide"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="30dp"
        android:layout_marginRight="30dp"
        android:src="@mipmap/ic_guide_from" />

</com.example.itservice.myapplication.DragSlideLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,401评论 25 709
  • 6、View的绘制 (1)当测量好一个View之后,我们就可以简单的重写 onDraw()方法,并在 Canvas...
    b5e7a6386c84阅读 5,896评论 0 3
  • 阴着的天 落着的雨 那时的你 此刻的我 那么远的距离 这么近的想念 不过一个阴天 只是一个阴天 此生足矣
    小蜜蜂嘉心阅读 1,706评论 0 1
  • 三十岁的年纪,我还能为我的兴趣爱好投入多少?这是今天我问自己的一个问题。当我问自己这个问题的时候,忽然有一种冷冷的...
    寒夜旋林阅读 3,848评论 0 0
  • 庭院小憩间,看到旁边的旧木板上有一小黑色物体在动,不自觉的离近了看,原来是一只蚂蚁拖着一只苍蝇的尸体在前行。只...
    芒果冰沙沙阅读 5,060评论 1 0

友情链接更多精彩内容