一丶首先创建一个手势库 二丶手势实例 布局文件:
在布局文件中有: <android.gesture.GestureOverlayView android:id="@+id/gv" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:gestureStrokeWidth="10" android:gestureColor="#ff0000" /> MainActivity中 public class MainActivity extends Activity { private GestureOverlayView gv;//手势控件 private GestureLibrary gestureLibrary;//加载手势库 private boolean loadStatus; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //拿到控件 gv = (GestureOverlayView) findViewById(R.id.gv); //创建加载手势库的工具 gestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures); //加载手势库 loadStatus = gestureLibrary.load(); //给gv加一个监听器 //监听一种手势 gv.addOnGesturePerformedListener(new OnGesturePerformedListener() { @Override public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { //手势加载成功 if(loadStatus){ //识别手势 Prediction(手势相似度) ArrayList<Prediction> pres = gestureLibrary.recognize(gesture); if(!pres.isEmpty()){ Prediction pre = pres.get(0); if(pre.score > 6){ if("close".equals(pre.name)){ finish(); }else if("tel".equals(pre.name)){ //打电话 Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel://110")); startActivity(intent); }else if("right".equals(pre.name)){ Toast.makeText(getApplicationContext(), "正确", 0).show(); } }else{ Toast.makeText(getApplicationContext(), "手势不匹配", 0).show(); } }else{ Toast.makeText(MainActivity.this, "手势库加载失败", 0).show() ; } } } }); } }
|