package com.yue.view;


import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * 作者：YMI on 2017/10/30 11:00
 * 邮箱：18702631465@163.com
 * QQ:1078561230
 */

public class DragViewGroup  extends LinearLayout {
    private int lastX,lastY,screenWidth,screenHeight;

    public DragViewGroup(Context context) {
        this(context, null);
    }

    public DragViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("NewApi") public DragViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;//减去下边的高度
    }
    //定位
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        //可以在这里确定这个viewGroup的：宽 = r-l.高 = b - t
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

        	lastX = (int) event.getRawX();
        	lastY = (int) event.getRawY();

           
            break;
        case MotionEvent.ACTION_MOVE:
        
                // 计算偏移量
                int dx = (int) (event.getRawX() - lastX);
                int dy = (int) (event.getRawY() - lastY);

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

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

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

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

                layout(left, top, right, bottom);

                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                invalidate();
//            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
//            layoutParams.leftMargin = (int)xDistance;
//            layoutParams.topMargin = (int)yDistance;
//            v.setLayoutParams(layoutParams);

            break;
    }
    	return true;
    	
    	
    	
    }
    
//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent event) {
//        int action = event.getAction() & MotionEvent.ACTION_MASK;
//        //为了使手指按在Button等可点击的控件上任可以滑动，需要拦截滑动实践
//        //并且为了使坐标准确，在此处记录按下的点
//        switch (action) {
//            case MotionEvent.ACTION_MOVE:
//                return true;
//            case MotionEvent.ACTION_DOWN:
//            	lastX =(int) event.getRawX();
//            	lastY= (int) event.getRawY();
//
//                return false;
//        }
//        return false;
//    }

}
