首页 > 代码库 > 17、手势(Gesture)

17、手势(Gesture)

课程目标:

学习Android必不可少的手势的功能 

了解手势识别原理 , 掌握制作,加载以及识别手势

写出自己的手势Demo

重点难点:手势机制的了解     手势库的制作

考核目标:请说一下手势库的机制 ,同时类似推演语音识别机制

 

二、手势的原理

(1)使用GuesturesBuilder创建手势库

    生成手势文件到:/sdcard/gestures

(2)加载手势库

把生成的文件放到res/raw下面,这就是手势库文件,供加载匹配

mLibrary = GestureLibraries.fromRawResource(this, R.raw.spells);

if (!mLibrary.load()) {

    finish();

}

(3)识别手势

 1 1, 2 <android.gesture.GestureOverlayView 3     android:id="@+id/gestures" 4     android:layout_width="fill_parent"  5     android:layout_height="0dip" 6     android:layout_weight="1.0" /> 7 2, 8 GestureOverlayView gestures = (GestureOverlayView) 9    findViewById(R.id.gestures);10 gestures.addOnGesturePerformedListener(this);11 12 3,13 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {14     ArrayList<prediction> predictions = mLibrary.recognize(gesture);15 16     // We want at least one prediction17     if (predictions.size() > 0) {18         Prediction prediction = predictions.get(0);19         // We want at least some confidence in the result20         if (prediction.score > 1.0) {21             // Show the spell22             Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();23         }24     }25 }

(4)Gestures overlay

【自己独立View识别】

 

【嵌套其它View】

 1 And here is what the XML layout looks like: 2  3 <android.gesture.GestureOverlayView 4     xmlns:android="http://schemas.android.com/apk/res/android" 5     android:id="@+id/gestures" 6     android:layout_width="fill_parent" 7     android:layout_height="fill_parent" 8      9     android:gestureStrokeType="multiple"10     android:eventsInterceptionEnabled="true"11     android:orientation="vertical">12 13     <ListView14         android:id="@android:id/list"  15         android:layout_width="fill_parent" 16         android:layout_height="fill_parent"  />17 18 </android.gesture.GestureOverlayView>19 In this application, the gestures view is an overlay on top of a regular ListView. The overlay also specifies a few properties that we did not need before:20 21 gestureStrokeType: indicates whether we want to recognize gestures made of a single stroke or multiple strokes. Since one of our gestures is the "+" symbol, we need multiple strokes22 eventsInterceptionEnabled: when set to true, this property tells the overlay to steal the events from its children as soon as it knows the user is really drawing a gesture. This is useful when there‘s a scrollable view under the overlay, to avoid scrolling the underlying child as the user draws his gesture23 orientation: indicates the scroll orientation of the views underneath. In this case the list scrolls vertically, which means that any horizontal gestures (like action_delete) can immediately be recognized as a gesture. Gestures that start with a vertical stroke must contain at least one horizontal component to be recognized. In other words, a simple vertical line cannot be recognized as a gesture since it would conflict with the list‘s scrolling.24 The code used to load and set up the gestures library and overlay is exactly the same as before. The only difference is that we now check the name of the predictions to know what the user intended to do:25 26 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {27     ArrayList<Prediction> predictions = mLibrary.recognize(gesture);28     if (predictions.size() > 0 && predictions.get(0).score > 1.0) {29         String action = predictions.get(0).name;30         if ("action_add".equals(action)) {31             Toast.makeText(this, "Adding a contact", Toast.LENGTH_SHORT).show();32         } else if ("action_delete".equals(action)) {33 Toast.makeText(this, "Removing a contact",34    Toast.LENGTH_SHORT).show();35         } else if ("action_refresh".equals(action)) {36 Toast.makeText(this, "Reloading contacts",37  Toast.LENGTH_SHORT).show();38         }39     }40 }

三、Case:练习手势的制作以及加载和识别

GestureLibrary store = GestureLibraries

.fromFile("/sdcard/gestures");

store.addGesture(textView.getText().toString(), gesture);

store.save(); 

四、Sample&Case:实现手势来跳转Activity

通过手势来跳转Activity

建立5个Activity ,分别放置一张大图 

向右滑动是下一个Activity ,向左滑动是上一个Activity ,向上滑动是到第一个Activity , 向下滑动是到最后一个Activity

 

17、手势(Gesture)