Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not to move, when has Button onClick. #29

Open
zhuyz89 opened this issue Apr 20, 2015 · 3 comments
Open

Can not to move, when has Button onClick. #29

zhuyz89 opened this issue Apr 20, 2015 · 3 comments

Comments

@zhuyz89
Copy link

zhuyz89 commented Apr 20, 2015

When has Button onClick, can not to move.

@nowavewater
Copy link

Hope this is not too late.
The window and the view (button) you try to add click listener are not exactly the same thing. What you need to do is the pass the touch event from button to the floating window. You may also want to detect the click from touch event instead of using click listener. Check the code below.

        private static final int MAX_CLICK_DURATION = 600;
        private static final int MAX_CLICK_DISTANCE = 5;
        private long pressStartTime;
        private float pressedX;
        private float pressedY;
        private boolean stayedWithinClickDistance;

        someView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                onTouchHandleMove(SomeWindow.DEFAULT_ID, getWindow(SomeWindow.DEFAULT_ID), view, motionEvent);
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        pressStartTime = System.currentTimeMillis();
                        pressedX = motionEvent.getX();
                        pressedY = motionEvent.getY();
                        stayedWithinClickDistance = true;
                        break;
                    }
                    case MotionEvent.ACTION_MOVE: {
                        if (stayedWithinClickDistance && distance(pressedX, pressedY, motionEvent.getX(), motionEvent.getY()) > MAX_CLICK_DISTANCE) {
                            stayedWithinClickDistance = false;
                        }
                        break;
                    }
                    case MotionEvent.ACTION_UP: {
                        long pressDuration = System.currentTimeMillis() - pressStartTime;
                        if (pressDuration < MAX_CLICK_DURATION && stayedWithinClickDistance) {
                            // Click event has occurred
                        }
                    }
                }
                return true;
            }
        });

        private  float distance(float x1, float y1, float x2, float y2) {
            float dx = x1 - x2;
            float dy = y1 - y2;
            float distanceInPx = (float) Math.sqrt(dx * dx + dy * dy);
            return pxToDp(distanceInPx);
        }

        private  float pxToDp(float px) {
            return px / getResources().getDisplayMetrics().density;
        }

@zhuyz89
Copy link
Author

zhuyz89 commented Dec 1, 2017

Although the answer a little late, but still thank you for your answer, thank you!

@zhuyz89
Copy link
Author

zhuyz89 commented Dec 1, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants