1、建立手势库,利用Android samples 里边的GestureBuilder项目实现(后附源码),完成后把gestures文件拷到项目的raw文件夹下
2、应用代码:
package cn.itcast.gesture;
import java.util.ArrayList;
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.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
private boolean success;
private GestureLibrary library;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//找到手势库
library = GestureLibraries.fromRawResource(this, R.raw.gestures);
//加载手势库
success = library.load();
GestureOverlayView gestureView = (GestureOverlayView)this.findViewById(R.id.gestures);
gestureView.addOnGesturePerformedListener(new GestureListener());
}
private final class GestureListener implements OnGesturePerformedListener{
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
if(success){
//从手势库中查找匹配的手势,最匹配的记录会放在最前面
ArrayList<Prediction> predictions = library.recognize(gesture);
if(!predictions.isEmpty()){
Prediction prediction = predictions.get(0);
Log.i("MainActivity", "score:"+ prediction.score);
if(prediction.score>3){
if("agree".equals(prediction.name)){
android.os.Process.killProcess(android.os.Process.myPid());
}else if("5556".equals(prediction.name)){
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:5556"));
startActivity(intent);
}
}
}
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.gesture.GestureOverlayView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:gestureStrokeType="multiple"
android:id="@+id/gestures"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.gesture"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>
GestureBuilder:http://pan.baidu.com/share/link?shareid=274431&uk=1796216265
项目源码:http://pan.baidu.com/share/link?shareid=274433&uk=1796216265