需要能够捏缩放,但不要在我的触控听器中进行双击

时间:2017-08-23 13:37:40

标签: android ontouchlistener ontouchevent motionevent android-touch-event

所以我在我看来有一个观点,我需要传递给捏缩放。但是,如果用户双击,我不想将双击传递给我的第二个视图。因为我需要在双击时设置的功能被忽略。 唯一的问题是该功能设置为在从触摸侦听器接收的2个ACTION_DOWN事件上调用。 现在我试图制作一个逻辑,以防止双重工作,但让缩放工作,但它仍然不完美。如果我用2个手指轻敲,屏幕上的1合1位置,然后另一个位置轻敲另一个位置,再过一点就可以轻松点击它,而不是消耗Touch,因为我需要。 这是我的touchEventListener的代码:

viewTop.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.i("","double touch event action: ===========================");
            Log.i("","double touch event test action:" + event.getPointerId(0));
            if(event.getPointerId(0) == 0) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (secondPressed) {
                        Log.i("", "double touch test : FIRST FINGER: CONSUMED TRUE");
                        timeLastTouch = System.currentTimeMillis();
                        return true;
                    }else {
                        Log.i("", "double touch DOWN : " + timeLastTouch + "... time passed: " + (System.currentTimeMillis() - timeLastTouch) + "..... location diff: " + (event.getX() - xLastTouch));
                        Log.i("", "double touch test XLAST : " + xLastTouch + "/" + yLastTouch + "   ... XLAST 2 : " + xLastTouch2 + "/" + yLastTouch2);
                        if (System.currentTimeMillis() - timeLastTouch < 1000 && Math.abs(event.getX() - xLastTouch) < 150 && Math.abs(event.getY() - yLastTouch) < 150) {
                            Log.i("", "double touch test TRUE DOWN");
                            secondPressed = true;
                            timeLastTouch = System.currentTimeMillis();
                            xLastTouch = event.getX();
                            yLastTouch = event.getY();
                            return true;
                        } else if (System.currentTimeMillis() - timeLastTouch < 1000 && Math.abs(event.getX() - xLastTouch2) < 150 && Math.abs(event.getY() - yLastTouch2) < 150) {
                            Log.i("", "double touch test TRUE DOWN");
                            secondPressed = true;
                            timeLastTouch = System.currentTimeMillis();
                            return true;
                        } else {
                            timeLastTouch = System.currentTimeMillis();
                            if(xLastTouch2 == -1) {
                                xLastTouch2 = event.getX();
                                yLastTouch2 = event.getY();
                            }else {
                                xLastTouch = event.getX();
                                yLastTouch = event.getY();
                            }
                            Log.i("", "double touch test FALSE DOWN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! X : " + event.getX() + "/" + event.getY() + ".... TIME: " + (System.currentTimeMillis() - timeLastTouch));
                            Log.i("", "double touch test FALSE DOWN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DIFF LAST TOUCH: " + Math.abs(event.getX() - xLastTouch) + "/" + Math.abs(event.getY() - yLastTouch));
                            Log.i("", "double touch test FALSE DOWN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DIFF LAST TOUCH2: " + Math.abs(event.getX() - xLastTouch2) + "/" + Math.abs(event.getY() - yLastTouch2));
                            return false;
                        }
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    handlerTouch.removeCallbacksAndMessages(null);
                    Log.i("", "double touch test FALSE UP");
                    timeLastTouch = System.currentTimeMillis();
                    handlerTouch.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            resetTouches();
                            Log.i("", "double touch test : SECOND PRESSED BECOMES FALSE");
                        }
                    }, 300);
                    return true;
                } else {
                    timeLastTouch = System.currentTimeMillis();
                    xLastTouch = event.getX();
                    yLastTouch = event.getY();
                    Log.i("", "double touch ELSE FALSE");
                    return false;
                }
            }else if(event.getPointerId(0) == 1){
                timeLastTouch = System.currentTimeMillis();
                if(event.getAction() == MotionEvent.ACTION_DOWN) {
                    xLastTouch2 = event.getX();
                    yLastTouch2 = event.getY();
                    secondPressed = true;
                    return false;
                }else if(event.getAction() == MotionEvent.ACTION_UP){
                    Log.i("","double touch test : SECONDARY FINGER: SECOND PRESSED BECOMES FALSE");
                    handlerTouch.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            resetTouches();
                            Log.i("", "double touch test : SECOND PRESSED BECOMES FALSE");
                        }
                    }, 300);
                }else {
                    Log.i("","double touch test : SECONDARY FINGER: SECOND PRESSED BECOMES TRUE");
                    secondPressed = true;
                }
                return false;
            }else {
                timeLastTouch = System.currentTimeMillis();
                secondPressed = true;
                return false;
            }
        }
    });

其中resetTouches是:

    private void resetTouches() {
    xLastTouch = -1f;
    yLastTouch = -1f;
    xLastTouch2 = -1f;
    yLastTouch2 = -1f;
    secondPressed = false;
}

现在我在这里被封锁,并且不知道改变什么或如何改变以防止双击工作。是否有任何变化可能更容易实现?我可以改变什么改进呢?

1 个答案:

答案 0 :(得分:0)

这对我有用:

final ScaleGestureDetector mScaleDetector = new ScaleGestureDetector(InCallActivity.this, new ScaleGestureDetector.OnScaleGestureListener() {
        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            Log.i("", "double touch test2 onScaleEnd: " + detector.getScaleFactor());
            app.TouchEvent(0, 1, (int) xFinger1, (int) yFinger1);
            if (xFinger2 != 0) {
                app.TouchEvent(1, 1, (int) xFinger2, (int) yFinger2);
            } else scale(1, (int) (yFinger1));
        }
        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            Log.i("", "double touch test2 onScaleBegin: " + detector.getScaleFactor());
            app.TouchEvent(0, 0, (int) xFinger1, (int) yFinger1);
            if (xFinger2 != 0) {
                app.TouchEvent(1, 0, (int) xFinger2, (int) yFinger2);
            } else app.TouchEvent(1, 0, (int) xFinger1, (int) yFinger1);
            return true;
        }
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            Log.i("", "double touch test2 zoom ongoing, scale: " + detector.getScaleFactor());
            app.TouchEvent(0, 2, (int) xFinger1, (int) yFinger1);
            if (xFinger2 != 0) {
                app.TouchEvent(1, 2, (int) xFinger2, (int) yFinger2);
            } else {
                scaleValue = detector.getScaleFactor();
                scale(2, (int) (yFinger1));
            }
            return false;
        }
    });
    viewTop.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.i("", "double touch test2 ON TOUCH: " + event.getPointerId(0) + "x/y: " + event.getX() + "/" + event.getY());
            xFinger1 = event.getX(0);
            yFinger1 = event.getY(0);
            try {
                xFinger2 = event.getX(1);
                yFinger2 = event.getY(1);
                mScaleDetector.onTouchEvent(event);
            } catch (Exception e) {
                return logicFor1Finger(event, e);
            }
            return true;
        }
    });