第一步:建立手势库
使用SDK自带例子GestureBuilder建立手势库(位置:android-sdk-windows\samples\android-8\GestureBuilder)。使用GestureBuilder之前,你需要恢复其到开发环境,然后进行编绎并部署到手机上。此时,就可以使用GestureBuilder建立手势库,生成的手势库文件在SCDard上,默认文件名称为:gestures
第二步:在应用中加载手势库文件,然后开发手势识别代码。
把手势库文件gestures文件拷贝到项目的res/raw目录下。然后在布局文件中添加用于手势绘制的View:
<android.gesture.GestureOverlayView android:id="@+id/gestures" android:layout_width="fill_parent“ android:layout_height="0dip" android:layout_weight="1.0" />
为View添加手势监听事件:gestureOverlayView.addOnGesturePerformedListener();
得到手势库:mLibrary =GestureLibraries.fromRawResource(this, R.raw.gestures);
加载手势库:mLibrary.load();
List<Prediction>predictions = mLibrary.recognize(gesture);//从手势库中查询匹配的内容,匹配的结果可能包括多个相似的内容,匹配度高的结果放在最前面
大多数情况下,手势都是通过一笔完成。然而有一些特别的需求就需要通过多个笔画来实现,这时可以使用gestureStrokeType属性进行设置:android:gestureStrokeType="multiple"
手势识别代码
public class MainActivity extends Activity { private GestureOverlayView gestureOverlayView; private GestureLibrary mLibrary; private boolean state; private EditText addressText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addressText = (EditText)this.findViewById(R.id.address); gestureOverlayView = (GestureOverlayView)this.findViewById(R.id.gestures); //当用户完成一次Gesture绘制后,系统将自动调用Listener对象的onGesturePerformed()方法 gestureOverlayView.addOnGesturePerformedListener(new GestureListener()); mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures); state = mLibrary.load();//加载手势库 } private final class GestureListener implements GestureOverlayView.OnGesturePerformedListener{ @Override public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { if(state){ List<Prediction> predictions = mLibrary.recognize(gesture);//从手势库中查询匹配的内容,匹配的结果可能包括多个相似的结果,匹配度高的结果放在最前面 if(!predictions.isEmpty()){ Prediction prediction = predictions.get(0); //prediction的score属性代表了与手势的相似程度 //prediction的name代表手势对应的名称 if(prediction.score > 1){ addressText.setText(prediction.name); } } } } } }
1、 将gesture文件放入工程res/raw/
2、 字符串资源
<string name="norecohnize">不能识别该手势</string> <string name="nopediction">手势识别百分率太低,请重新输入</string>
3、 布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.gesture.GestureOverlayView android:id="@+id/myGresture" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1.0" /> </LinearLayout>
4、 MyGestureTestActivity
package cn.class3g.gesture; … public class MyGestureTestActivity extends Activity { GestureOverlayView gestureView = null; GestureLibrary libary = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init() { gestureView = (GestureOverlayView) this.findViewById(R.id.myGresture); gestureView .addOnGesturePerformedListener(new MyOnGesturePerformedListener()); // 创建手势库对象 libary = GestureLibraries.fromRawResource(this, R.raw.gestures); // 加载手势资源 libary.load(); } private final class MyOnGesturePerformedListener implements OnGesturePerformedListener { public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { ArrayList<Prediction> predictions = libary.recognize(gesture); if (!predictions.isEmpty()) { Prediction prediction = predictions.get(0); Log.i("TAG", String.valueOf(prediction.score)); if (prediction.score > 5) { // 用户输入手势与手势库中保存手势相似度大于10% if ("close".equals(prediction.name)) { finish(); } else if ("dialto".equals(prediction.name)) { Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:133123145678")); startActivity(intent); } } else { showToast(R.string.norecohnize); } } else { showToast(R.string.nopediction); } } } private void showToast(int resId) { Toast.makeText(this, resId, Toast.LENGTH_LONG).show(); } }