Google Glass GDK Tap Gesture

时间:2013-11-22 18:16:00

标签: google-glass google-gdk

使用GDK和Google提供的示例代码,TAP手势不会被识别为TAP。每次都以LONG_PRESS的形式返回。以下是代码:

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

}

我在这里遗漏了什么或这是一个错误吗?

3 个答案:

答案 0 :(得分:2)

如果您只是想要为UI捕捉点击事件(不使用GestureDetector和所有内容),请在Glass触摸板点击中心点击d-pad,这样您就可以直接拦截KEYCODE_DPAD_CENTER按键。

试试这个:

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

    return false;
}

答案 1 :(得分: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 :(得分:0)

从GDK复制并粘贴GestureDetector代码并修改它是您应该做的所有事情。如果它正在双击,那么我怀疑你可能在Glass上遇到了一些硬件问题。

你有没有试过为Gesture.TAP做一个Toast?也许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;
        }