通过手势编辑组件android.gesture.GestureOverlayView来编辑手势,使用代码如下:
<android.gesture.GestureOverlayView
android:id="@+id/gesture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gestureStrokeType="multiple"/>
为组件绑定事件监听器:
<span style="white-space:pre"> </span>gestureView.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener(){
<span style="white-space:pre"> </span>public void onGesturePerformed(GestureOverlayView overlay,final Gesture gesture){}
<span style="white-space:pre"> </span>}
设置手势编辑时的一些属性:
<span style="white-space:pre"> </span>gestureView.setGestureColor(Color.RED);
gestureView.setGestureStrokeWidth(4);
保存手势:
GestureLibrary gestureLib = GestureLibraries.fromFile("/mnt/sdcard/mygestures");
// 手势库添加手
gestureLib.addGesture(gestureName.getText().toString(),gesture);
gestureLib.save();
将手势转为Bitmap :
//gesture.Bitmap(int width,int height,int inset,int color);
<span style="white-space:pre"> </span>Bitmap bitmap = gesture.toBitmap(128,128,10,0xffff0000);
加载手势库:
// 加载手势库
gestureLib = GestureLibraries.fromFile("/mnt/sdcard/mygestures");
// 判断手势加载是否成功
if(gestureLib.load()){
Toast.makeText(RecognizeGesture.this, "读取手势库成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(RecognizeGesture.this, "读取手势库失败", Toast.LENGTH_SHORT).show();
}
识别手势:
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
ArrayList<String> result = new ArrayList<String>();
for(Prediction pred:predictions){
if(pred.score > 2){
result.add("与手势"+pred.name+"相似度为:"+pred.score);
}
}