Android基础开发之手势识别

2022-10-19,

这篇文章主要为大家详细介绍了Android基础开发之手势识别的相关资料,感兴趣的小伙伴们可以参考一下

由于精确度等原因,手势识别在android中用的并不多,不过这并不妨碍我们来玩玩这个神奇的玩意。

在android中要使用手势,先得建立手势库,建立手势库非常简单,新建一个android sample project,建一个android示例工程,然后选择创建的android版本,完了之后看到这个界面:

选择gesturebuilder,创建成功之后把它安装到真机上,然后可以在里边添加手势,并给手势命名。

创建完gesture之后,在eclipse的file explore窗口中查看系统文件,在sdcard文件夹中会多出一个gesture文件,先把这个文件导出到桌面。然后新建一个名叫gesture的工程,在res文件夹下新建一个raw文件夹,再把刚才的gesture文件拷贝进来,这样我们这着工程就有了一个手势库了,下面看看怎么用这个手势库。

先看看布局文件:

<RelativeLayout 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"
 tools:context="com.example.gesture.MainActivity" >

 <android.gesture.GestureOverlayView
 android:id="@+id/mygesture"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gestureStrokeType="multiple"
  />

</RelativeLayout>

布局文件中就一个关于gestureOverlayView的控件,android:gestureStrokeType属性有两个值,一个是multiple,另一个是single,multiple表示支持多笔画,single表示支持单笔画。

Java代码:

public class MainActivity extends Activity {

 private GestureOverlayView myges;
 private GestureLibrary library;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 myges = (GestureOverlayView) this.findViewById(R.id.mygesture);
 library = GestureLibraries.fromRawResource(this,
  R.raw.gestures);
 // 读取库中数据
 library.load();

 //监听绘制手势事件
 myges.addOnGesturePerformedListener(new OnGesturePerformedListener() {

  @Override
  public void onGesturePerformed(GestureOverlayView overlay,
   Gesture gesture) {
  //Prediction中存储的是比对的结果
  List<Prediction> list = library.recognize(gesture);
  //遍历结果,score是比对后的分数,分数越大,越相近
  for(Prediction p : list){
   Log.i("lenve", p.name+"------------"+p.score);
  }
  if(list.get(0).score>4){
   Toast.makeText(MainActivity.this,list.get(0).name, Toast.LENGTH_LONG).show();
  }else{
   Toast.makeText(MainActivity.this,"手势无法识别", Toast.LENGTH_LONG).show();
  }
  }
 });
 }
}

关键代码已注释。就这么简单,由于识别率等问题,手势识别目前用的并不多。

原文链接:http://blog.csdn.net/u012702547/article/details/45727729

源码下载:Android开发之手势识别

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持北冥有鱼。

您可能感兴趣的文章:

  • Android通过滑动实现Activity跳转(手势识别器应用)
  • Android手势识别器GestureDetector使用详解
  • 札记:android手势识别功能实现(利用MotionEvent)
  • Android View进行手势识别详解
  • Android应用开发中触摸屏手势识别的实现方法解析
  • android开发之为activity增加左右手势识别示例
  • android创建手势识别示例代码
  • android使用gesturedetector手势识别示例分享
  • 理解Android的手势识别提高APP的用户体验
  • Android使用GestureOverlayView控件实现手势识别

《Android基础开发之手势识别.doc》

下载本文的Word格式文档,以方便收藏与打印。