Android(安卓)Gesture 手势识别使用实例
16lz
2021-01-25
很高兴能在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代码- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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>
第三步:载入Gesture:
Java代码- mLibrary=GestureLibraries.fromRawResource(this,R.raw.gestures);
- if(!mLibrary.load()){
- finish();
- }
第四步:增加响应函数OnGesturePerformedListener:
Java代码- GestureOverlayViewgestures=(GestureOverlayView)findViewById(R.id.gestures);
- gestures.addOnGesturePerformedListener(this);
以上四步就可以实现简单的Gesture识别原型了:
程序运行结果如下,书写一个a字,程序识别出,然后toast一个a出来:
完整代码如下:
Java代码- packagecom.ray.test;
- importjava.util.ArrayList;
- importandroid.app.Activity;
- importandroid.gesture.Gesture;
- importandroid.gesture.GestureLibraries;
- importandroid.gesture.GestureLibrary;
- importandroid.gesture.GestureOverlayView;
- importandroid.gesture.Prediction;
- importandroid.gesture.GestureOverlayView.OnGesturePerformedListener;
- importandroid.os.Bundle;
- importandroid.widget.Toast;
- publicclassTestGestureextendsActivityimplementsOnGesturePerformedListener{
- GestureLibrarymLibrary;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- GestureOverlayViewgestures=(GestureOverlayView)findViewById(R.id.gestures);
- gestures.addOnGesturePerformedListener(this);
- mLibrary=GestureLibraries.fromRawResource(this,R.raw.gestures);
- if(!mLibrary.load()){
- finish();
- }
- }
- @Override
- publicvoidonGesturePerformed(GestureOverlayViewoverlay,Gesturegesture){
- ArrayListpredictions=mLibrary.recognize(gesture);
- //Wewantatleastoneprediction
- if(predictions.size()>0){
- Predictionprediction=(Prediction)predictions.get(0);
- //Wewantatleastsomeconfidenceintheresult
- if(prediction.score>1.0){
- //Showthespell
- Toast.makeText(this,prediction.name,Toast.LENGTH_SHORT).show();
- }
- }
- }
- }
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- Android(安卓)使用View Gone 与 ViewStub的区别
- android 存储方法一SharedPreferences存储
- 【android】第一行代码CoolWeather中遇到的问题
- 每天学习一个Android中的常用框架——15.RxJava
- android 调用 python
- Kotlin概述之Kotlin for Android
- Xamarin发布XobotOS源码,C# 实现的Android系统 - 操作系统 - ITey
- Eclipse下Android的代码调试