---
title: "android 之GestureOverlayView手势识别"
author: "卡菲牛奶"
date: "2013-05-28T00:18:10Z"
tags: []
categories: ["移动开发"]
slug: "android-gestureoverlayview"
draft: false
lastmod: "2013-05-28T00:26:16Z"
id: 1877248
url: "https://fuchangle.iteye.com/blog/1877248"
---

android 之GestureOverlayView可以轻松实现手势比划的识别，使用自己的手势库与之匹配，下面是一个小例子：
首先打开android SDK安装路径，还原AndroidGestureDetector项目，此项目可以为我们生成手势库，并把手势库文件村房子SD卡的根路径下， 我的项目路径为<D:\android-sdk-windows\samples\android-10\GestureBuilder>，
注意，打开项目并为之添加三个文件才能添加到环境中，.classpath、.project、default.properties，这项文件到其他android项目中去拷贝就OK啦，
![](http://dl.iteye.com/upload/picture/pic/47399/53068318-4f51-33ef-810c-b431d066fd80.png)
然后在项目中录入手势和与之对应的名称，并把生成的手势库文件拷贝出来，添加至替他工程。

另建工程GesturesText

main.xml

```xml

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <android.gesture.GestureOverlayView
        android:id="@+id/gesture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
</LinearLayout>

```


string.xml

```xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">GesturesText</string>
    <string name="menu_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
	<string name="nofind">没有找到匹配项</string>
	<string name="low">匹配度太低</string>
</resources>

```


在工程res路径下新建raw文件夹，并将生成的手势库文件添加至文件夹。
MainActivity.java

```java

package com.gesturestext.example;

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.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private GestureLibrary libraries;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		libraries = GestureLibraries.fromRawResource(this, R.raw.gestures);
		libraries.load();
		GestureOverlayView overlayView = (GestureOverlayView)findViewById(R.id.gesture);
		overlayView.addOnGesturePerformedListener(new GesturePerformedListener());
	}
	
	private class GesturePerformedListener implements OnGesturePerformedListener{

		@Override
		public void onGesturePerformed(GestureOverlayView overlay,
				Gesture gesture) {
			ArrayList<Prediction> predictions = libraries.recognize(gesture);
			if(!predictions.isEmpty()){
				Prediction prediction = predictions.get(0);
				if(prediction.score >= 6){
					if("close".equals(prediction.name)){
						finish();
					}else if("fuchangle".equals(prediction.name)){
						Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:15001386673"));
						startActivity(intent);
					}
				}else {
					Toast.makeText(MainActivity.this, R.string.low, Toast.LENGTH_LONG).show();
				}
			}else {
				Toast.makeText(MainActivity.this, R.string.nofind, Toast.LENGTH_LONG).show();
			}
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		android.os.Process.killProcess(android.os.Process.myPid());
	}
}


```


注意添加打电话权限<uses-permission android:name="android.permission.CALL_PHONE"/>
工程代码见附件