这里主要介绍多笔手势识别(在文章的最后面会附上单笔手势识别的代码)
1、/res/raw/目录下导入手势库文件gesture,这个文件可以利用Android自带的GestireBuilder来绘制.
回执号的手势库会保存在/mnt/sdcard目录下。
2、main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.gesture.GestureOverlayView
android:id="@+id/gov"
android:gestureStrokeType="multiple"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="ok"
android:text="确定"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="cancel"
android:text="取消"
/>
</LinearLayout>
</LinearLayout>
3、MainActivity
package com.njupt.gesture1;
import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.Prediction;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private GestureOverlayView gov;
private GestureLibrary library;
private Gesture gesture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gov = (GestureOverlayView) findViewById(R.id.gov);
gov.addOnGestureListener(new MyOnGestureListener());
library = GestureLibraries.fromRawResource(getApplicationContext(), R.raw.gestures);
library.load();
}
private class MyOnGestureListener implements OnGestureListener{
@Override
public void onGestureStarted(GestureOverlayView overlay,
MotionEvent event) {
// TODO Auto-generated method stub
}
@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub
}
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
gesture = overlay.getGesture();
}
@Override
public void onGestureCancelled(GestureOverlayView overlay,
MotionEvent event) {
// TODO Auto-generated method stub
}
}
public void ok(View v){
recognize(gesture);
gov.clear(true);
}
public void cancel(View v){
gov.clear(true);
}
public void recognize(Gesture gesture){
ArrayList<Prediction> predictions = library.recognize(gesture);
if(predictions.isEmpty()){
Toast.makeText(getApplicationContext(), "手势不匹配", 1).show();
}else{
Prediction prediction = predictions.get(0);
double score = prediction.score;
if(score >= 5){
String name = prediction.name;
if(name.equals("call")){
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+5556));
startActivity(intent);
}else if(name.equals("close")){
android.os.Process.killProcess(android.os.Process.myPid());
}
}else{
Toast.makeText(getApplicationContext(), "匹配度太低.....", 1).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;
}
}
4、AndroidManifest.xml
因为生面的程序中用到了打电话的权限,所以要在清单文件中注册打电话的权限
<uses-permission android:name="android.permission.CALL_PHONE"/>
-------------------------------------------------以下附上但比手势识别的核心代码——————---------------------------------
gov.addOnGesturePerformedListener(new MyOnGesturePerformedListener());只针对单笔手势
private final class MyOnGesturePerformedListener implements OnGesturePerformedListener{
public void onGesturePerformed(GestureOverlayView overlay,
Gesture gesture) {
// TODO Auto-generated method stub
//识别手势
recognize(gesture);
}
}