Android开发之手势识别

一.首先创建手势库

方法:去Android SDK samples下面 ,找到GestureBuilder项目.这里所有的文件放到自己创建的GestureBuilder里面.然后,运行到模拟器或者手机上,按步骤新建手势库.新建完会在sdcard上生成文件名为:gestures.拿出来备用.

(adb -s XXX pull /sdcard/gestures /XXX/guestures)

二.手势创建

1、创建绘图区GestureOverlayView overlay = (GestureOverlayView) findViewById(R.id.gestures_overlay);

overlay.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);

overlay.setFadeOffset(2000);  //多笔画每两次的间隔时间

overlay.setGestureColor(Color.CYAN);//画笔颜色

overlay.setGestureStrokeWidth(6);//画笔粗细值

overlay.addOnGestureListener(new GesturesProcessor());

2、监听绘制

private class GesturesProcessor implements GestureOverlayView.OnGestureListener {

public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {

mDoneButton.setEnabled(false);

mGesture = null;

}

public void onGesture(GestureOverlayView overlay, MotionEvent event) {

}

public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {

mGesture = overlay.getGesture();

if (mGesture.getLength() < LENGTH_THRESHOLD) {

overlay.clear(false);

}

mDoneButton.setEnabled(true);

}

public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {

}

}

3、保存手势

public void addGesture(View v) {

if (mGesture != null) {

final TextView input = (TextView) findViewById(R.id.gesture_name);

final CharSequence name = input.getText();

if (name.length() == 0) {

input.setError(getString(R.string.error_missing_name));

return;

}

/**

* 获取手势库

*   private final File mStoreFile = new File(Environment.getExternalStorageDirectory(), "gestures");

*   GestureLibrary sStore = GestureLibraries.fromFile(mStoreFile);

*

*/

final GestureLibrary store = GestureBuilderActivity.getStore();

store.addGesture(name.toString(), mGesture);

store.save();

setResult(RESULT_OK);

final String path = new File(Environment.getExternalStorageDirectory(),

"gestures").getAbsolutePath();

Toast.makeText(this, getString(R.string.save_success, path), Toast.LENGTH_LONG).show();

} else {

setResult(RESULT_CANCELED);

}

finish();

}

三、手势识别

public class GesturePerformedActivity extends Activity {private final File mStoreFile = new File(

Environment.getExternalStorageDirectory(), "gestures");

// 手势库

GestureLibrary mGestureLib;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.gesture_perform);

// 手势画板

GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures_overlay);

gestures.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);

 

gestures.setFadeOffset(2000); // 多笔画每两次的间隔时间

gestures.setGestureColor(Color.CYAN);// 画笔颜色

gestures.setGestureStrokeWidth(6);// 画笔粗细值

 

// 手势识别的监听器

gestures.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {

@Override

public void onGesturePerformed(GestureOverlayView overlay,

Gesture gesture) {

// 从手势库中查询匹配的内容,匹配的结果可能包括多个相似的结果,匹配度高的结果放在最前面

ArrayList<Prediction> predictions = mGestureLib

.recognize(gesture);

if (predictions.size() > 0) {

Prediction prediction = (Prediction) predictions.get(0);

// 匹配的手势

if (prediction.score > 1.0) { // 越匹配score的值越大,最大为10

Toast.makeText(GesturePerformedActivity.this,

prediction.name, Toast.LENGTH_SHORT).show();

}

}

}

});

 

if (mGestureLib == null) {

mGestureLib = GestureLibraries.fromFile(mStoreFile);

mGestureLib.load();

}

}

}

效果图

b0edc67d12e182

发表评论

您必须 登录 才能发表留言!