悬浮扫描全屏跑

/**
 * 可拖拽的view
 */
public class DragButton extends AppCompatTextView {

    private int myWidth;
    private int myHeight;
    //父控件的宽高
    private int parentWidth;
    private int parentHeight;

    //是否拖动
    private boolean isDrag = false;

    /**
     * 父控件
     */
    private ViewGroup parentView;

    public boolean isDrag() {
        return isDrag;
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        myWidth = getMeasuredWidth();
        myHeight = getMeasuredHeight();
        if (parentHeight == 0 || parentWidth == 0) {
            parentView = (ViewGroup) getParent();
            parentView.post(new Runnable() {
                @Override
                public void run() {
                    parentWidth = parentView.getMeasuredWidth();
                    parentHeight = parentView.getMeasuredHeight();
                }
            });
        }
    }

    private float downX;
    private float downY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (this.isEnabled()) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    isDrag = false;
                    downX = event.getX();
                    downY = event.getY();

                    break;
                case MotionEvent.ACTION_MOVE:

                    final float xDistance = event.getX() - downX;
                    final float yDistance = event.getY() - downY;
                    int l, r, t, b;
                    //当水平或者垂直滑动距离大于10,才算拖动事件
                    if (Math.abs(xDistance) > 10 || Math.abs(yDistance) > 10) {

                        isDrag = true;
                        //在父控件中左边的位置 = 当前在父控件中的位置 - 内边距 + 拖动的距离
                        l = (int) (getLeft() - parentView.getPaddingLeft() + xDistance);
                        //在父控件中的右边位置 = 在左边的位置 + 自己的宽
                        r = l + myWidth;
                        t = (int) (getTop() - parentView.getPaddingTop() + yDistance);
                        b = t + myHeight;
                        if (l < 0) {
                            //如果左边的距离超出父控件的左边有效范围,那么这个值会是负数,
                            // 为了保证在父控件中的拖动,所以设为父控件的左边的起点(包括内边距)
                            l = parentView.getPaddingLeft();
                            r = l + myWidth;
                        } else if (r > (parentWidth - parentView.getPaddingRight())) {
                            r = parentWidth - parentView.getPaddingRight();
                            l = r - myWidth;
                        }
                        if (t < 0) {
                            t = parentView.getPaddingTop();
                            b = t + myHeight;
                        } else if (b > parentHeight - parentView.getPaddingBottom()) {
                            b = parentHeight - parentView.getPaddingBottom();
                            t = b - myHeight;
                        }

                        this.layout(l, t, r, b);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (isDrag) {
                        //如果是拖动,就拦截点击事件
                        return false;
                    }
                    break;
            }
            return super.onTouchEvent(event);
        }
        return false;
    }

}

对应使用该自定义控件的utils:

/**
 * 管理DragButton工具类
 */

public class DragButtonUtil {

    private static DragButton dragButton;

    public  static DragButton getDragButton(){

        return dragButton;
    }


    public static void  createDragButton(Activity activity,int gravity){
        FrameLayout frameLayout = new FrameLayout(activity);

        dragButton = new DragButton(activity,null);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams((int)activity.getResources().getDimension(R.dimen.x269),(int)activity.getResources().getDimension(R.dimen.y299));
        //Gravity.BOTTOM | Gravity.RIGHT
        lp.gravity = gravity;
        lp.bottomMargin=139;
        dragButton.setLayoutParams(lp);
        frameLayout.addView(dragButton);
        ViewGroup.LayoutParams params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        activity.getWindow().addContentView(frameLayout,params);
    }


    /**
     * 创建属于该activity的DragButton
     * @param activity
     * @return
     */
    public static void  createDragButton(Activity activity, int x, int y,int gravity){
        FrameLayout frameLayout = new FrameLayout(activity);
        dragButton = new DragButton(activity,null);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(x,y);

        //Gravity.BOTTOM | Gravity.RIGHT
        lp.gravity = gravity;
        lp.bottomMargin=139;
        dragButton.setLayoutParams(lp);
        frameLayout.addView(dragButton);
        ViewGroup.LayoutParams params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        activity.getWindow().addContentView(frameLayout,params);
    }

    public static void  hideDragButton(){
        dragButton.setVisibility(View.GONE);
    }

    public static void  showDragButton(){
        dragButton.setVisibility(View.VISIBLE);
    }
}

你可能感兴趣的:(安卓)