Android实现View的任意拖动


安卓实现View的拖曳有几种方法,都不外乎改变View的坐标,并且重绘,于是,只要能够改变之就可以。


可以自定义View,重写OnTouchEvent(MotionEvent event),可以在Activity 的onTouch()方法中监听手指在屏幕上的位置变化实现,也可以设置View的OnTouch监听。


自定义View的方式:


import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 *自定义的view,能够随意拖动。
 *
 */

public class DragView extends ImageView {
    public DragView(Context context) {
        super(context);
    }

    public DragView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

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

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public DragView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private float downX;
    private float downY;

    private long downTime;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (this.isEnabled()) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    downX = event.getX();
                    downY = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    final float xDistance = event.getX() - downX;
                    final float yDistance = event.getY() - downY;
                    if (xDistance != 0 && yDistance != 0) {
                        int l = (int) (getLeft() + xDistance);
                        int r = (int) (getRight() + xDistance);
                        int t = (int) (getTop() + yDistance);
                        int b = (int) (getBottom() + yDistance);
                        this.layout(l, t, r, b);

                    }
                    break;
                case MotionEvent.ACTION_UP:
                    setPressed(false);
                    break;
                case MotionEvent.ACTION_CANCEL:
                    setPressed(false);
                    break;
            }
            return true;
        }
        return false;
    }

}


通过设置onTouch监听的方式,封装成工具类,方便重用:


import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * 这个工具可以使任何一个view进行拖动。
 * 
 */

public class DragViewUtil {
    public static void drag(View v){
        drag(v,0);
    }

    /**
     * 拖动View方法
     * @param v view
     * @param delay 延迟
     */
    public static void drag(View v,long delay){
        v.setOnTouchListener(new TouchListener(delay));
    }
    private static class  TouchListener  implements View.OnTouchListener {
        private float downX;
        private float downY;
        private long downTime;
        private long delay;
        private TouchListener() {
        }
        private TouchListener(long delay) {
            this.delay = delay;
        }
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    downX = event.getX();
                    downY = event.getY();
                    downTime = System.currentTimeMillis();
                    break;
                case MotionEvent.ACTION_MOVE:
                    if(System.currentTimeMillis() - downTime>= delay){
                    final float xDistance = event.getX() - downX;
                    final float yDistance = event.getY() - downY;
                    if (xDistance != 0 && yDistance != 0) {
                        int l = (int) (v.getLeft() + xDistance);
                        int r = (int) (v.getRight() + xDistance);
                        int t = (int) (v.getTop() + yDistance);
                        int b = (int) (v.getBottom() + yDistance);
                        v.layout(l, t, r, b);
                        ViewGroup.LayoutParams p = v.getLayoutParams();
                    }
                    }
                    break;
            }
            return false;
        }

        }
}

调用方法:

View view = findViewById(R.id.xxx);

DragViewUtil.drag(view);



运行预览:


Android实现View的任意拖动_第1张图片



项目地址:https://github.com/Mingyueyixi/DragView


你可能感兴趣的:(Android,android)