Android-手势识别(自定义手势识别:四种自定义手势)

来源:互联网 发布:卖鸽子的软件 编辑:程序博客网 时间:2024/05/20 16:12

1.回顾

  上篇实现:

   (1)OnTouchListener 实现 上下左右手势识别

   (2)OnTouchListener + SimpleOnGestureListener + GestureDetector 实现 上下左右 手势识别

2.重点

   (1)GestureOverlayView + GestureLibrary + OnGesturePerformedListener 实现 自定义手势识别

   (2)demo 下载

3.效果图

                                                           


 4. 基本步骤

   (1)自定义手势识别文件

   (2)加载 手势文件

   (3)布局实现

   (4)识别/读取 手势 

    

5. 自定义手势文件

   5.1 新建 Android Simple Project 

        以eclipse为例:File  -> New -> Other -> Android -> Android Simple Project ;

                                       

    5.2 Next 选择安卓版本

                                       

     5.3 Next 选择 GestureBuilder

                                       

    5.4 finish 

        右击 运行工程 -> 运行后 点击 -> add gesture -> 画上你自己定义的手势 -> 起个名字(记住 需要使用)-> done ;


           


   5.5 取得 gestrue 手势文件

    当点击done ->  显示 Toast 提醒 文件地址 -> 保存在sdcard 里,自己导出即可 ;


6.加载gestrue 手势文件

  (1)在  res 文件夹下 新建 raw 文件夹;

  (2)复制gesture 到raw 文件夹下 ;

  (3)在onCreate 里加载

private GestureLibrary library;

//加载手势文件    library=GestureLibraries.fromRawResource(MainActivity.this,R.raw.gestures);library.load();

7. 布局实现

    (1)在 布局文件中 添加 GestureOverlayView ,可以 将你自己的 控件 放在 布局里面;


                                 

    (2)实例:

<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="${relativePackage}.${activityClass}" >    <android.gesture.GestureOverlayView        android:id="@+id/gestureOverlayView1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_centerInParent="true"        >                  <ImageView        android:id="@+id/img_test"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_centerInParent="true"          android:layout_gravity="center"        android:src="@drawable/icon_location" />          <TextView              android:id="@+id/textView1"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_marginTop="20dp"             android:layout_gravity="center|top"              android:text="没有滑动"              android:textColor="#0000ff"              android:textSize="25sp" />            </android.gesture.GestureOverlayView></RelativeLayout>


8. 识别手势

   (1)实现OnGesturePerformedListener 监听

   

/** * 第三中方式 自定义方式 *  *///设置监听class gestureOverlayListener implements OnGesturePerformedListener{@Overridepublic void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {//识别手势: 通过 library 读取手势文件 ,在这里读取ArrayList<Prediction> predictions=library.recognize(gesture);//去第一个就 是 取到的第一个Prediction prediction=predictions.get(0);//提示值String str="没有改手势";//更加相似度 来 取得 区间(0.0~10.0 大致区间)if(prediction.score>=5.0){//通过 name 来判断 值if(prediction.name.equals("error")){str="error:很遗憾,错的!";img_test.setImageResource(R.drawable.icon_error);}else if(prediction.name.equals("light")){str="light:我是闪电";img_test.setImageResource(R.drawable.icon_light);}else if(prediction.name.equals("none")){str="none :什么都没有";img_test.setImageResource(R.drawable.icon_none);}else if(prediction.name.equals("right")){str="true : 恭喜你答对了!";img_test.setImageResource(R.drawable.icon_true);}}    textView1.setText(str);Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();}}

    (2)初始化控件 和 添加监听

gestureOverlayView1=(GestureOverlayView) findViewById(R.id.gestureOverlayView1);//添加gestureOverlayView1.addOnGesturePerformedListener(new gestureOverlayListener());

9.demo 下载

http://download.csdn.net/detail/lablenet/9063867


0 0
原创粉丝点击