<android.gesture.GestureOverlayView android:id="@+id/gesture" android:layout_width="fill_parent" android:layout_height="fill_parent" />2)GestureOverlayView注册OnGestureListener实现onGesturesStarted, onGesture, onGestureEnded方法
5) 使用gesturelibrary.addGesture("xxx", gesture) 将gesture与字符串绑定
2. 使用手势
<android.gesture.GestureOverlayView android:id="@+id/gesture" android:layout_width="fill_parent" android:layout_height="fill_parent" />若设成
<android.gesture.GestureOverlayView android:id="@+id/gesture" android:layout_width="fill_parent" android:layout_height="fill_parent" />则报错。
GestureLibrary library = GestureLibraries.fromRawResource(this,R.raw.gestures);//设置手势库路径 library.load();//加载手势库得到手势屏幕
GestureOverlayView overlayView =(GestureOverlayView) this.findViewById(R.id.gesture);设置单笔手势监听器:
overlayView.addOnGesturePerformedListener(newGestureOverlayView.OnGesturePerformedListener(){ public void onGesturePerformed(GestureOverlayViewoverlay, Gesture gesture) { //用户写的手势与手势库对比,得到对比结果 ArrayList<Prediction> list=library.recognize(gesture); if(list!=null){ Prediction p = list.get(0);//取得最符合的手势结果 if(p.score>5){//对比度越大相似度越高 Toast.makeText(getApplicationContext(), p.name, 1).show(); } else{ Toast.makeText(getApplicationContext(), "匹配不成功", 1).show(); } } } });多划处理
在xml中设置手势的触屏界面 <android.gesture.GestureOverlayView android:id="@+id/gesture" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gestureStrokeType="multiple" <!-此句是设置使用多笔处理-> />将监听器该为
overlayView.addOnGestureListener(new OnGestureListener(){ @Override public void onGesture(GestureOverlayView overlay, MotionEventevent) { } public void onGestureCancelled(GestureOverlayViewoverlay, MotionEvent event) { } public void onGestureEnded(GestureOverlayViewoverlay, MotionEvent event) { } public void onGestureStarted(GestureOverlayViewoverlay, MotionEvent event) { gesture = overlay.getGesture();//保存在后一次的屏幕图 } });