首页 > 代码库 > Google glass GDK - 手势操作

Google glass GDK - 手势操作

发现在使用多个卡片或者其他啥的时候,onKeyDown事件不起作用,所以弄清楚了GDK的手势操作,写个笔记记录记录~

我们先创建一个卡片手势检测器

1     // 创建卡片2     private Card mCard;3     // 手势检测器4     private GestureDetector mGestureDetector;

然后在onCreate方法中初始化我们的变量

1     protected void onCreate(Bundle savedInstanceState) {2         super.onCreate(savedInstanceState);3         //setContentView(R.layout.activity_main);4         mCard = new Card(this);5         mGestureDetector = createGestureDetector(this);6         mCard.setText("手势识别");7         setContentView(mCard.getView());8     }
createGestureDetector是我们新建的一个函数,用于实例化手势检测器并绑定事件
 1     private GestureDetector createGestureDetector(Context context){ 2         GestureDetector gestureDetector = new GestureDetector(context); 3         // 4         // 基本的按键事件 5         // 6         gestureDetector.setBaseListener(new GestureDetector.BaseListener() { 7             @Override 8             public boolean onGesture(Gesture arg0) { 9                 // TODO Auto-generated method stub10                 // 轻拍11                 if(arg0 == Gesture.TAP){12                     mCard.setText("一个手指轻拍=>播放|暂停");13                 }else if(arg0 == Gesture.TWO_TAP){14                     // 双指轻拍15                     mCard.setText("两个手指轻拍=>弹出菜单");16                 }else if(arg0 == Gesture.LONG_PRESS){17                     // 长按18                     mCard.setText("长按=>退出");19                 }else if(arg0 == Gesture.SWIPE_LEFT){20                     mCard.setText("向后=>上一首");21                 }else if(arg0 == Gesture.SWIPE_RIGHT){22                     mCard.setText("向前=>下一首");23                 }else if(arg0 == Gesture.SWIPE_DOWN){24                     mCard.setText("向下=>后台运行");25                 }else{26                     return false;27                 }28                 setContentView(mCard.getView());29                 return true;30             }31         });32         //33         // 监听手指数34         //35         gestureDetector.setFingerListener(new GestureDetector.FingerListener() {36             37             @Override38             public void onFingerCountChanged(int arg0, int arg1) {39                 // TODO Auto-generated method stub40                 System.out.println("手指:原先=" + arg0 + " 当前:" + arg1);41             }42         });43         //44         // 滚动事件45         //46         gestureDetector.setScrollListener(new GestureDetector.ScrollListener() {47             48             @Override49             public boolean onScroll(float arg0, float arg1, float arg2) {50                 // TODO Auto-generated method stub51                 System.out.println("滚动:arg0 = " + arg0 + " arg1=" + arg1 + " arg2="+ arg2);52                 return false;53             }54         });55         return gestureDetector;56     }

最后,我们要将事件发送到手势检测器

1     @Override2     public boolean onGenericMotionEvent(MotionEvent event){3         if(mGestureDetector != null){4             return mGestureDetector.onMotionEvent(event);5         }6         return false;7     }