mainActivity如下:

package cn.c;import java.io.File;import java.util.ArrayList;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.os.Bundle;import android.os.Environment;import android.view.KeyEvent;import android.view.View;import android.view.View.OnKeyListener;import android.widget.Button;import android.widget.EditText;/** * 需求描述: * 在例子1的基础上增加手势识别. * 所以不用再写添加手势的步骤. * 错误总结: * 在手势识别的时候没有执行mGestureLibrary.load() * 导致mGestureLibrary.recognize()方法失败. * 该错误很类似于刚开始操作 SharedPreferences的时候 * Editor没有执行commit()方法 * */public class MainActivity extends Activity {    private EditText mEditText;    private GestureOverlayView mGestureOverlayView;    private GestureLibrary mGestureLibrary;    private Gesture mGesture;    private Button mSaveButton;    private Button mResetButton;    private String mGestureLibraryPath;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        init();    }private void init() {mEditText = (EditText) findViewById(R.id.editText);mEditText.setOnKeyListener(new OnKeyListener() {public boolean onKey(View v, int keyCode, KeyEvent event) {if (mGesture!=null&&mEditText.getText().toString().length()>0) {mSaveButton.setEnabled(true);} else {mSaveButton.setEnabled(false);}return false;}});mGestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView);mSaveButton = (Button) findViewById(R.id.saveButton);mSaveButton.setEnabled(false);mResetButton = (Button) findViewById(R.id.resetButton);//GestureLibrary文件的路径mGestureLibraryPath =Environment.getExternalStorageDirectory()+File.separator+"gesturesTest";//为GestureOverlayView添加监听事件mGestureOverlayView.addOnGesturePerformedListener(new GesturePerformedListenerImpl());}private class GesturePerformedListenerImpl implements OnGesturePerformedListener{public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {// 加载该路径下的手势库.// 若不存在,则会在路径下创建一个手势库mGestureLibraryPath =Environment.getExternalStorageDirectory()+File.separator+"gesturesTest";mGestureLibrary = GestureLibraries.fromFile(mGestureLibraryPath);//加载手势库//一定要执行load()方法,否则recognize()没有用!if (mGestureLibrary.load()) {ArrayList<Prediction> predictionsList=mGestureLibrary.recognize(gesture);if (predictionsList.size()>0) {Prediction bestPrediction=predictionsList.get(0);if (bestPrediction.score>7.0) { System.out.println("xxxx1111 该手势已经存在");}} else {              System.out.println("xxxx1111 该手势不存在");}} else {System.out.println("xxxx1111 加载手势库失败");}}}}


main.xml如下:

<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" >    <LinearLayout        android:id="@+id/linearLayout"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:orientation="horizontal" >        <TextView            android:id="@+id/textView"            android:layout_width="100dip"            android:layout_height="wrap_content"            android:text="手势的名称:" />        <EditText            android:id="@+id/editText"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:hint="在此输入" />    </LinearLayout> <android.gesture.GestureOverlayView     android:id="@+id/gestureOverlayView"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_below="@id/linearLayout"     android:layout_marginBottom="50dip"     android:gestureStrokeType="multiple"  />  <LinearLayout        android:layout_width="fill_parent"        android:layout_height="50dip"        android:layout_alignParentBottom="true"        android:orientation="horizontal" >    <Button         android:id="@+id/saveButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"         android:text="保存"        />     <Button         android:id="@+id/resetButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"         android:text="重置"        />   </LinearLayout> </RelativeLayout>


更多相关文章

  1. android 加载大图的原理和实现
  2. Android左右滑动切换图片
  3. android greenDao 编译问题总结
  4. Android录音,拍照,摄像
  5. Android实现手势缩放图片
  6. Android实现手势检测
  7. java 编译中常用函数
  8. Android中的人机交互技术
  9. 为何Android普通APP可以执行私有数据中的so文件,而system app却不

随机推荐

  1. 今天发现了一个不错的android加载动画
  2. Android流媒体开发之路二:NDK C++开发And
  3. Android中实现上下左右都可滑动的ScrollV
  4. android TextView图文混写 嵌入表情
  5. Android startActivityForResult基本用法
  6. golang服务器+android 生成相同md5码
  7. android activity 单例 切换
  8. Android笔试和面试常见题目(四)
  9. JNI log来代替printf打印调试
  10. ubuntu android eclipse 链接设备(手机)看l