Android 手势
//定义布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <android.gesture.GestureOverlayView android:id="@+id/gestureOverlayView_main" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
public class MainActivity extends Activity { private GestureOverlayView gestureOverlayView = null; private void initView() { gestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView_main); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); // 添加 // gestureOverlayView // .addOnGesturePerformedListener(new OnGesturePerformedListener() { // // // 手势绘制完毕回调 // @Override // public void onGesturePerformed(GestureOverlayView overlay, // Gesture gesture) { // // TODO Auto-generated method stub // // 手势库 // // 获取手势库对象的方法 // GestureLibrary gestureLibrary = GestureLibraries.fromFile(Environment // .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) // + File.separator + "shoushi"); // // 要给每一个手势命名,也就是在一个手势库当中,可以还有多个手势 // gestureLibrary.addGesture("wuxing", gesture); // // // 把已经在手势库对象中的手势保存到文件当中,返回值代表成功失败 // boolean flag = gestureLibrary.save(); // if (flag) { // Toast.makeText(getApplicationContext(), "添加成功", // Toast.LENGTH_SHORT).show(); // }else{ // Toast.makeText(getApplicationContext(), "添加失败", // Toast.LENGTH_SHORT).show(); // } // } // }); // 匹配 gestureOverlayView .addOnGesturePerformedListener(new OnGesturePerformedListener() { @Override public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { // TODO Auto-generated method stub GestureLibrary gestureLibrary = GestureLibraries.fromFile(Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator + "shoushi"); // 如果加载成功了,就开始匹配逻辑。 if (gestureLibrary.load()) { // 用一个已知的手势去寻找可以匹配的手势,返回成一个手势的集合 ArrayList<Prediction> list = gestureLibrary .recognize(gesture); if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { Toast.makeText( getApplicationContext(), list.get(i).name + ":" + list.get(i).score, Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(), "没有匹配到", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(), "加载失败", Toast.LENGTH_SHORT).show(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }