0

I am detecting a two-finger tap vs. a two-finger scroll in Android. The feedback from the motion events is such that the order is either:

    2 fingers down (repeated many times)
    1 finger down   (repeated a few times)
    back to 2 fingers down (this indicates a longer hold than a tap. In my case we'll call this scrolling)

or

    2 fingers down
    1 finger down
    no action for a few milliseconds, which will indicate that there has been a quick 2 finger tap. 

Basically, I want my code to do this: If there is 1 finger down and there have been 2 fingers down before, wait for a few milliseconds to see whether another finger returns or if nothing happens. If another finger returns, stop waiting. If nothing happens, there was a tap.

public boolean onGenericMotionEvent(MotionEvent event) {

        int numFingers = event.getPointerCount();
        switch (event.getActionIndex()){
            case (MotionEvent.ACTION_DOWN):
                if (waitThread != null){
                    stateChange=true;
                }
                if (numFingers==2){
                    hasHadTwoDown=true;
                }
                if (numFingers==1){
                    //FIRE A THREAD THAT WAITS
                    if (hasHadTwoDown){
                        waitThread = new WaitThread();
                        waitThread.run();
                    }
                }
        }

        gestureDetector.onTouchEvent(event);

}

and the thread

 private class WaitThread extends Thread {



    public WaitThread(){
        stateChange=false;
    }

    @Override
    public void run(){

        Log.i("myGesture", "thread is running");
        long startTime = Calendar.getInstance().getTimeInMillis();


            while(!stateChange && Calendar.getInstance().getTimeInMillis() - startTime < 100){
                //wait until getting a notification that state changes or timeout 
            }

            if (stateChange){
                waitThread=null;
                                    //no double click
                return;
            }

        //double click
        Log.i("myGesture", "double click");
        hasHadTwoDown=false;
        waitThread=null;
    }


}

currently, the thread runs to completion and doesn't receive any notifications from the MotionEvent. MotionEvents don't go through while the thread is running. What sort of synchronization should I be using?

edit: I have completed a working version of the project. It's available at https://github.com/ctuna/MultiTouch

4

1 回答 1

1

使用 waitThread.start,而不是 .run。

于 2013-07-09T01:43:34.223 回答