Android Gesture 手势识别使用实例

来源:互联网 发布:代理服务器软件 编辑:程序博客网 时间:2024/05/20 20:05

 很高兴能在Android1.6的sdk看到手势识别这一功能,之前一直在想,如何在android中实现nds游戏那样用手势(准确点应该是笔势)来控制游戏角色?现在总算看到一点曙光了,不过手势要做到笔势那样随心所欲地控制游戏人物,还有很多细节问题需要处理。

    在Android1.6的模拟器里面预装了一个叫Gestures Builder的程序,这个程序就是让你创建自己的手势的(Gestures Builder的源代码在sdk问samples里面有,有兴趣可以看看)。创建的手势将被保存到/sdcard/gestures里面,把这个文件复制到你的工程/res/raw下,你就可以在你的工程里面使用这些手势了。复制到/res/raw下的手势是只读的,也就是说你不能修改或增加手势了,如果想实现增改的话,可以直接加载sd卡里面的gestures文件。

    在例子中,我创建了这样的手势:

 

第二步:在layout里面创建GestureOverlayView,这个透明的view就是让你在上面画手势用的,可以叠在其他View上面:

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12. <android.gesture.GestureOverlayView  
  13.     android:id="@+id/gestures"  
  14.     android:layout_width="fill_parent"   
  15.     android:layout_height="0dip"  
  16.     android:layout_weight="1.0"   
  17.     />  
  18. </LinearLayout>  

 

 第三步:载入Gesture:

 

Java代码  收藏代码
  1. mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);  
  2. if (!mLibrary.load()) {  
  3.     finish();  
  4. }  

 

 第四步:增加响应函数OnGesturePerformedListener

 

Java代码  收藏代码
  1. GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);  
  2. gestures.addOnGesturePerformedListener(this);  

 

 以上四步就可以实现简单的Gesture识别原型了:

程序运行结果如下,书写一个a字,程序识别出,然后toast一个a出来:

完整代码如下:

Java代码  收藏代码
  1. package com.ray.test;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.gesture.Gesture;  
  7. import android.gesture.GestureLibraries;  
  8. import android.gesture.GestureLibrary;  
  9. import android.gesture.GestureOverlayView;  
  10. import android.gesture.Prediction;  
  11. import android.gesture.GestureOverlayView.OnGesturePerformedListener;  
  12. import android.os.Bundle;  
  13. import android.widget.Toast;  
  14.   
  15. public class TestGesture extends Activity implements OnGesturePerformedListener{  
  16.   
  17.     GestureLibrary mLibrary;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);  
  23.         gestures.addOnGesturePerformedListener(this);  
  24.         mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);  
  25.         if (!mLibrary.load()) {  
  26.             finish();  
  27.         }  
  28.     }  
  29.   
  30.     @Override  
  31.     public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {  
  32.         ArrayList predictions = mLibrary.recognize(gesture);  
  33.   
  34.         // We want at least one prediction  
  35.         if (predictions.size() > 0) {  
  36.             Prediction prediction = (Prediction) predictions.get(0);  
  37.             // We want at least some confidence in the result  
  38.             if (prediction.score > 1.0) {  
  39.                 // Show the spell  
  40.                 Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();  
  41.             }  
  42.         }  
  43.           
  44.     }  
  45. }  

 文章参考了android博客上面的这篇文章:

http://feedproxy.google.com/~r/blogspot/hsDu/~3/Rrgh3YnIqig/gestures-on-android-16.html

原创粉丝点击