XML代码:
<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/gestureOverlayView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="24dp" android:text="CheckBox" /> </RelativeLayout> </android.gesture.GestureOverlayView>
JAVA代码:
import java.util.ArrayList; import android.os.Bundle; import android.app.Activity; import android.gesture.Gesture; import android.gesture.GestureLibraries; import android.gesture.GestureLibrary; import android.gesture.GestureOverlayView; import android.gesture.GestureOverlayView.OnGesturePerformedListener; import android.gesture.Prediction; import android.view.Menu; import android.widget.CheckBox; public class MainActivity extends Activity { /** 1) 定义手势 导入并运行GestureBuilder 导出mnt/sdcard/gestures 2) 识别手势 a) gestures文件拷贝到res\raw\ b) layout 新增GestureOverlayView,用来做布局 c) 加载手势库 final GestureLibrary library = GestureLibraries.fromRawResource(this, R.raw.gestures); library.load(); d) 识别手势 GestureOverlayView OverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView1); OnGesturePerformedListener listener = new OnGesturePerformedListener() { @Override public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { ArrayList<Prediction> recognizeList = library.recognize(gesture); Prediction prediction = recognizeList.get(0); if(prediction.score >= 5)//相似度score取值范围0-10 { if(prediction.equals("check")) { checkBox.setChecked(true); } } } }; OverlayView.addOnGesturePerformedListener(listener); */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1); //获得手势库 final GestureLibrary library = GestureLibraries.fromRawResource(this, R.raw.gestures); library.load();//导入库 //找到和监听手写view GestureOverlayView OverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView1); OnGesturePerformedListener listener = new OnGesturePerformedListener() { @Override public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { ArrayList<Prediction> recognizeList = library.recognize(gesture); Prediction prediction = recognizeList.get(0); if(prediction.score >= 5)//相似度score取值范围0-10 { if(prediction.name.equals("check"))//判断name是否相同 { checkBox.setChecked(true); } } } }; OverlayView.addOnGesturePerformedListener(listener); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }