package com.yue.util;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;


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

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

    /**
     * 拖动View方法
     *
     * @param v     view
     * @param delay 延迟
     */
    public static void drag(Context con, View v, long delay) {
        v.setOnTouchListener(new TouchListener(con,delay));
    }

    private static class TouchListener implements View.OnTouchListener {
        private float startX;
        private float startY;
        private long downTime;
        private long delay;
        private int screenWidth = 0;
        private int screenHeight = 0;

        private TouchListener() {
        }

        private TouchListener(Context con, long delay) {
            screenWidth = con.getResources().getDisplayMetrics().widthPixels;
            screenHeight = con.getResources().getDisplayMetrics().heightPixels;
            this.delay = delay;
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                    startX = event.getRawX();
                    startY = event.getRawY();

                    downTime = System.currentTimeMillis();
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (System.currentTimeMillis() - downTime >= delay) {
                        // 计算偏移量
                        int dx = (int) (event.getRawX() - startX);
                        int dy = (int) (event.getRawY() - startY);

                        // 计算控件的区域
                        int left = v.getLeft() + dx;
                        int right = v.getRight() + dx;
                        int top = v.getTop() + dy;
                        int bottom = v.getBottom() + dy;

                        // 超出屏幕检测
                        if (left < 0) {
                            left = 0;
                            right = v.getWidth();
                        }

                        if (right > screenWidth) {
                            right = screenWidth;
                            left = screenWidth - v.getWidth();
                        }
                        if (top < 0) {
                            top = 0;
                            bottom = v.getHeight();
                        }

                        if (bottom > screenHeight) {
                            bottom = screenHeight;
                            top = screenHeight - v.getHeight();
                        }

                        v.layout(left, top, right, bottom);

                        startX = event.getRawX();
                        startY = event.getRawY();

//                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
//                    layoutParams.leftMargin = (int)xDistance;
//                    layoutParams.topMargin = (int)yDistance;
//                    v.setLayoutParams(layoutParams);

                    }
                    break;
            }

            return false;
        }
    }
}
