How to implement a two-finger double-click in Android?

前端 未结 2 1150
情歌与酒
情歌与酒 2020-12-30 13:37

I know how to detect a double-click and a two-finger touch event, but how can I combine these to react so somebody needs to double click with two fingers?

By default

2条回答
  •  执笔经年
    2020-12-30 14:03

    This is a double-click listener I created to detect a two-finger double click.

    Variables used:

    private GestureDetector gesture;
    private View.OnTouchListener gestureListener;
    boolean click1 = false;
    boolean click2 = false;
    long first = 0;
    long second = 0;
    

    In the activity's onCreate() to register the touch events:

    gesture = new GestureDetector(getApplicationContext(), new SimpleOnGestureListener(){
        public boolean onDown(MotionEvent event) {
            return true;
        }
    });
    gestureListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gesture.onTouchEvent(event);
        }
    };
    

    Outside of onCreate() inside the activity:

    @Override
    public boolean onTouchEvent(MotionEvent event) {   
        try {
            int action = event.getAction() & MotionEvent.ACTION_MASK;
            //capture the event when the user lifts their fingers, not on the down press
            //to make sure they're not long pressing
            if (action == MotionEvent.ACTION_POINTER_UP) {
                //timer to get difference between clicks
                Calendar now = Calendar.getInstance();
    
                //detect number of fingers, change to 1 for a single-finger double-click, 3 for a triple-finger double-click, etc.
                if (event.getPointerCount() == 2) {
                    if (!click1) {
                        //if this is the first click, then there hasn't been a second
                        //click yet, also record the time
                        click1 = true;
                        click2 = false;
                        first = now.getTimeInMillis(); 
                    } else if (click1) {
                        //if this is the second click, record its time 
                        click2 = true;
                        second = now.getTimeInMillis();
    
                        //if the difference between the 2 clicks is less than 500 ms (1/2 second)
                        //Math.abs() is used because you need to be able to detect any sequence of clicks, rather than just in pairs of two
                        //(e.g. click1 could be registered as a second click if the difference between click1 and click2 > 500 but
                        //click2 and the next click1 is < 500)
                        if (Math.abs(second-first) < 500) {
    
                            //do something!!!!!!
    
                        } else if (Math.abs(second-first) >= 500) {
                            //reset to handle more clicks
                            click1 = false;
                            click2 = false;
                        }
                    }
                }
            }
        } catch (Exception e){
    
        }
        return true;
    }
    

提交回复
热议问题