2013-11-22 83 views
5

由Google提供的GDK和示例代碼,TAP手勢未被識別爲TAP。每次都以LONG_PRESS的形式返回。下面是代碼:Google Glass GDK Tap Gesture

import com.google.android.glass.touchpad.Gesture; 
import com.google.android.glass.touchpad.GestureDetector; 

public class MainActivity extends Activity { 

Logger log = Logger.getLogger("MainActivity"); 

private GestureDetector mGestureDetector; 
// ... 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // ... 
    mGestureDetector = createGestureDetector(this); 
} 

private GestureDetector createGestureDetector(Context context) { 
GestureDetector gestureDetector = new GestureDetector(context); 
    //Create a base listener for generic gestures 
    gestureDetector.setBaseListener(new GestureDetector.BaseListener() { 
     @Override 
     public boolean onGesture(Gesture gesture) { 
      log.info(gesture.name()); 
      if (gesture == Gesture.TAP) { 
       // do something on tap 
       return true; 
      } else if (gesture == Gesture.TWO_TAP) { 
       // do something on two finger tap 
       return true; 
      } else if (gesture == Gesture.SWIPE_RIGHT) { 
       // do something on right (forward) swipe 
       return true; 
      } else if (gesture == Gesture.SWIPE_LEFT) { 
       // do something on left (backwards) swipe 
       return true; 
      } 
      return false; 
     } 
    }); 
    gestureDetector.setFingerListener(new GestureDetector.FingerListener() { 
     @Override 
     public void onFingerCountChanged(int previousCount, int currentCount) { 
      // do something on finger count changes 
     } 
    }); 
    gestureDetector.setScrollListener(new GestureDetector.ScrollListener() { 
     @Override 
     public boolean onScroll(float displacement, float delta, float velocity) { 
      // do something on scrolling 
     } 
    }); 
    return gestureDetector; 
} 

/* 
* Send generic motion events to the gesture detector 
*/ 
@Override 
public boolean onGenericMotionEvent(MotionEvent event) { 
    if (mGestureDetector != null) { 
     return mGestureDetector.onMotionEvent(event); 
    } 
    return false; 
} 

}

我失去了一些東西在這裏或者這是一個錯誤?

+0

您提供的版畫「TAP」對我來說,當我在觸摸板上簡單點擊代碼。你是否說過,當你做一個快速分裂的水龍頭時,它仍然打印出「LONG_PRESS」? –

+0

是的。儘可能快地嘗試嘗試,並且從未得到過「TAP」 – Patrick

+0

然後,假設您在Glass的其他部分沒有出現問題。出於好奇,你可以嘗試其他手勢,看看它們是否按預期工作,比如TWO_TAP和THREE_TAP,或者一些滑動? –

回答

0

從GDK複製並粘貼GestureDetector代碼並修改它就是您應該需要做的所有事情。如果它是雙擊工作,那麼我會懷疑你可能在Glass上遇到了一些硬件問題。

你有沒有嘗試做一個Toast的Gesture.TAP?也許TAP和LONG PRESS是一樣的?

當您點擊Glass時,以下代碼將調用generateCard()。

private GestureDetector createGestureDetector(Context context) { 
     GestureDetector gestureDetector = new GestureDetector(context); 
      //Create a base listener for generic gestures 
      gestureDetector.setBaseListener(new GestureDetector.BaseListener() { 
       @Override 
       public boolean onGesture(Gesture gesture) { 
        if (gesture == Gesture.TAP) { // On Tap, generate a new number 
         generateCard(); 
         return true; 
        } else if (gesture == Gesture.TWO_TAP) { 
         // do something on two finger tap 
         return true; 
        } else if (gesture == Gesture.SWIPE_RIGHT) { 
         // do something on right (forward) swipe 
         return true; 
        } else if (gesture == Gesture.SWIPE_LEFT) { 
         // do something on left (backwards) swipe 
         return true; 
        } 
        return false; 
       } 
      }); 
      gestureDetector.setFingerListener(new GestureDetector.FingerListener() { 
       @Override 
       public void onFingerCountChanged(int previousCount, int currentCount) { 
        // do something on finger count changes 
       } 
      }); 
      gestureDetector.setScrollListener(new GestureDetector.ScrollListener() { 
       @Override 
       public boolean onScroll(float displacement, float delta, float velocity) { 
        // do something on scrolling 

        return false; 
       } 
      }); 
      return gestureDetector; 
     } 

     /* 
     * Send generic motion events to the gesture detector 
     */ 
     @Override 
     public boolean onGenericMotionEvent(MotionEvent event) { 
      if (mGestureDetector != null) { 
       return mGestureDetector.onMotionEvent(event); 
      } 
      return false; 
     } 
1

我和你有同樣的問題,我的答案可能很奇怪,但我避免使用開關而不是if else結構。此外,通過這個新代碼,您將能夠捕獲其餘的手勢。

我希望它能幫助你。

private GestureDetector createGestureDetector(Context context){ 
    GestureDetector gestureDetector = new GestureDetector(context); 
    //Create a base listener for generic gestures 
    gestureDetector.setBaseListener(new GestureDetector.BaseListener() { 
     @Override 
     public boolean onGesture(Gesture gesture) { 
      Log.e(TAG,"gesture = " + gesture); 
      switch (gesture) { 
       case TAP: 
        Log.e(TAG,"TAP called."); 
        handleGestureTap(); 
        break; 
       case LONG_PRESS: 
        Log.e(TAG,"LONG_PRESS called."); 
        return true; 
       case SWIPE_DOWN: 
        Log.e(TAG,"SWIPE_DOWN called."); 
        return true; 
       case SWIPE_LEFT: 
        Log.e(TAG,"SWIPE_LEFT called."); 
        return true; 
       case SWIPE_RIGHT: 
        Log.e(TAG,"SWIPE_RIGHT called."); 
        return true; 
       case SWIPE_UP: 
        Log.e(TAG,"SWIPE_UP called."); 
        return true; 
       case THREE_LONG_PRESS: 
        Log.e(TAG,"THREE_LONG_PRESS called."); 
        return true; 
       case THREE_TAP: 
        Log.e(TAG,"THREE_TAP called."); 
        return true; 
       case TWO_LONG_PRESS: 
        Log.e(TAG,"TWO_LONG_PRESS called."); 
        return true; 
       case TWO_SWIPE_DOWN: 
        Log.e(TAG,"TWO_SWIPE_DOWN called."); 
        return true; 
       case TWO_SWIPE_LEFT: 
        Log.e(TAG,"TWO_SWIPE_LEFT called."); 
        return true; 
       case TWO_SWIPE_RIGHT: 
        Log.e(TAG,"TWO_SWIPE_RIGHT called."); 
        return true; 
       case TWO_SWIPE_UP: 
        Log.e(TAG,"TWO_SWIPE_UP called."); 
        return true; 
       case TWO_TAP: 
        Log.e(TAG,"TWO_TAP called."); 
        return true; 
      } 

      return false; 
     } 
    }); 
    gestureDetector.setFingerListener(new com.google.android.glass.touchpad.GestureDetector.FingerListener() { 
     @Override 
     public void onFingerCountChanged(int previousCount, int currentCount) { 
      // do something on finger count changes 
      Log.e(TAG,"onFingerCountChanged()"); 

     } 
    }); 
    gestureDetector.setScrollListener(new com.google.android.glass.touchpad.GestureDetector.ScrollListener() { 
     @Override 
     public boolean onScroll(float displacement, float delta, float velocity) { 
      // do something on scrolling 
      Log.e(TAG,"onScroll()"); 
      return false; 
     } 
    }); 
    return gestureDetector; 
} 
2

如果你只希望捕獲的UI點擊事件(不使用GestureDetector和一切),在玻璃觸摸板水龍頭被登記爲中心點擊d墊,所以你可以簡單地攔截KEYCODE_DPAD_CENTER關鍵印刷機。

試試這個:

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){ 
     // The touchpad was tapped 
     return true; 
    } 

    return false; 
} 
+0

這肯定是最快和最簡單的方法。不幸的是,這種方法不處理我已經閱讀的Swipe Left和Swipe Right命令應該在KEYCODE_TAB下注冊並且在KeyEvent.isShiftPressed() – SMT

+0

上有一個布爾標誌。我認爲那些只是映射到KEYCODE_DPAD_LEFT和KEYCODE_DPAD_RIGHT?也許我錯了。 –

+0

我也嘗試過這些,可悲的是它沒有奏效。最後,因爲我剛剛參加了GestureDetector路線,所以這是一個有爭議的問題。 – SMT