pingpongboss / StandOut

StandOut lets you easily create floating windows in your Android app.
http://pingpongboss.github.com/StandOut
MIT License
1.24k stars 379 forks source link

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

Open zhuyz89 opened 9 years ago

zhuyz89 commented 9 years ago

When has Button onClick, can not to move.

nowavewater commented 7 years ago

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 commented 6 years ago

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

zhuyz89 commented 6 years ago

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

On Tue, May 16, 2017 at 2:16 PM, Pengcheng Yin notifications@github.com wrote:

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;
    }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pingpongboss/StandOut/issues/29#issuecomment-301686035, or mute the thread https://github.com/notifications/unsubscribe-auth/ACszisj7XvwXHQZUdo23bDuZho6E00-Iks5r6T9VgaJpZM4EEOgB .