随手指滑动的悬浮窗 onTouchListener

private class FloatingOnTouchListener implements View.OnTouchListener {
        private int x;
        private int y;
        private int touchSlop;
        private boolean handleBySelf = false;
        private boolean isMoving = false;
    
        FloatingOnTouchListener(Context context) {
            ViewConfiguration vc = ViewConfiguration.get(context);
            touchSlop = vc.getScaledTouchSlop();
        }
    
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x = (int) event.getRawX();
                    y = (int) event.getRawY();
    
                    handleBySelf = false;
                    isMoving = false;
                    Helper.print("down time:%s", event.getDownTime());
                    break;
                case MotionEvent.ACTION_MOVE:
                    int nowX = (int) event.getRawX();
                    int nowY = (int) event.getRawY();
                    int movedX = nowX - x;
                    int movedY = nowY - y;
                    
                    x = nowX;
                    y = nowY;
    
                    if (movedX > touchSlop || movedY > touchSlop) {
                        handleBySelf = true;
                        isMoving = true;
                    }
                    mParams.x = mParams.x + movedX;
                    mParams.y = mParams.y + movedY; // 更新悬浮窗控件布局
                    mWindowManager.updateViewLayout(view, mParams);
                    break;
                case MotionEvent.ACTION_UP:
                    long time = event.getEventTime() - event.getDownTime();
                    if (!isMoving && ViewConfiguration.getLongPressTimeout() < time) {
                        // "it's long click"                        
                        handleBySelf = true;
                    }
                default:
                    break;
            }
    
            Helper.print("action:%s, event time:%s, x:%s, y:%s", ViewUtil.getActionName(event.getAction()), event.getEventTime(), mParams.x, mParams.y);
            return handleBySelf;
        }
    }

你可能感兴趣的:(android)