android 之GestureOverlayView可以轻松实现手势比划的识别,使用自己的手势库与之匹配,下面是一个小例子:
首先打开android SDK安装路径,还原AndroidGestureDetector项目,此项目可以为我们生成手势库,并把手势库文件村房子SD卡的根路径下, 我的项目路径为 D:\android-sdk-windows\samples\android-10\GestureBuilder,
注意,打开项目并为之添加三个文件才能添加到环境中,.classpath、.project、default.properties,这项文件到其他android项目中去拷贝就OK啦,
android 之GestureOverlayView手势识别
然后在项目中录入手势和与之对应的名称,并把生成的手势库文件拷贝出来,添加至替他工程。

另建工程GesturesText

main.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 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
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;@Overrideprotected 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{@Overridepublic 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();}}}@Overrideprotected void onDestroy() {super.onDestroy();android.os.Process.killProcess(android.os.Process.myPid());}}


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

更多相关文章

  1. Android编译过程总结及android中各种img文件的作用以及系统启动
  2. Android Studio项目引入外部库注意事项(PullToRefresh)
  3. Android 项目国际化 多国语言适配
  4. 用Eclipse开发Android项目
  5. android 混淆配置 实例实战项目讲解

随机推荐

  1. 字节跳动屏幕适配方案解读
  2. Android(安卓)wifi 信号强度单位 dbm
  3. Android屏幕录制并转换gif
  4. android校园二手市场客户端+服务端源代码
  5. Android通过 SharedPreference 实现用户
  6. android widget 开发实例 : 桌面便签程序
  7. Android资源管理框架(Asset Manager)简要介
  8. 浏览器唤起App
  9. Android(安卓)常用的距离单位
  10. Android(安卓)Studio 打包 Jar (将自己的