对于android中的手势识别,效果如下图所示:


模拟器中有个可以添加手势的工具Gestures Builder,可以添加手势如下图所示:


当添加手势成功后,会在Sdcard上产生保存的手势的文件gestures,如下图所示:


你需要把sdcard中的gestures文件拷贝到项目中去


手势识别的完整的代码如下:

package com.example.demo1;import java.util.ArrayList;import android.app.Activity;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.os.Bundle;import android.widget.Toast;public class MainActivity extends Activity implements OnGesturePerformedListener {GestureLibrary mLibrary;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);gestures.addOnGesturePerformedListener(this);mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);if (!mLibrary.load()) {finish();}}@Overridepublic void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {ArrayList predictions = mLibrary.recognize(gesture);// We want at least one predictionif (predictions.size() > 0) {Prediction prediction = (Prediction) predictions.get(0);// We want at least some confidence in the resultif (prediction.score > 1.0) {// Show the spellToast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();}}}}

布局文件的代码如下:
<?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"      >  <TextView        android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="@string/hello"      />  <android.gesture.GestureOverlayView      android:id="@+id/gestures"      android:layout_width="fill_parent"       android:layout_height="0dip"      android:layout_weight="1.0"       />  </LinearLayout>



更多相关文章

  1. 设置Activity大小不再全屏原理
  2. Your project contains error(s)... 如何解决?
  3. Android系统集成第三方pre-build库和程序(转)
  4. android PreferenceActivity结合PreferenceFragment实现参数设置
  5. Android(安卓)ART
  6. Android:基于Eclipse编译调试系统级应用源代码
  7. 新手在路上! Android(安卓)UI界面设计
  8. Android按钮文字变色(使用 ColorStateList)
  9. Android(安卓)开发中,有哪些坑需要注意?

随机推荐

  1. Android自用-----Intent Action 大全
  2. 桌面花园(MQTT—NodeMCU—Android实践)
  3. Android -- View流程
  4. Android录屏命令screenrecord命令的使用
  5. Gradle配置文件详解
  6. 【Android】自定义环形菜单View
  7. Android系统源码framework SystemUI导入e
  8. 学习_网站链接
  9. Android应用libGDX引擎系列(一)-Android
  10. Android学习笔记:Android基础知识点(不断更