StandOut icon indicating copy to clipboard operation
StandOut copied to clipboard

Can not to move, when has Button onClick.

Open zhuyz89 opened this issue 9 years ago • 3 comments

When has Button onClick, can not to move.

zhuyz89 avatar Apr 20 '15 11:04 zhuyz89

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



nowavewater avatar May 16 '17 06:05 nowavewater

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

zhuyz89 avatar Dec 01 '17 05:12 zhuyz89

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 [email protected] 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 .

zhuyz89 avatar Dec 01 '17 06:12 zhuyz89