Android入门(54)——第九章 使用GestureOverlayView进行手势识别

来源:互联网 发布:取消淘宝账号实名认证 编辑:程序博客网 时间:2024/06/18 10:31

1. 简介:


2. 需要用到Gesture Builder:

第一步:通过new将Gesture Builder添加到项目中:不过前提是你要找到它在哪里。


第二步:打开后:


第三步:然后安装在虚拟手机上


第四步:添加手势:Add gesture:


第五步:手势gestrue保存地址:


然后需要将其导出并放置在目标项目Project中使用即可:


3. 具体导入方法:

第一步:在res文件中创建文件夹raw:


第二步:ctrl+c再ctrl+v将手势文件放进raw文件中。


4. 使用:


第一步:布局文件:GestrueOverlayView控件:





<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <android.gesture.GestureOverlayView        android:id="@+id/gestureOverlayView1"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >        <ImageView            android:id="@+id/img"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_centerHorizontal="true"            android:src="@drawable/ic_launcher" />    </android.gesture.GestureOverlayView></RelativeLayout>
第二步:MainActivity文件:

package com.example.gesturedetectodemo;import java.util.ArrayList;import android.os.Bundle;import android.app.Activity;import android.gesture.Gesture;import android.gesture.GestureLibraries;import android.gesture.GestureLibrary;import android.gesture.GestureOverlayView;import android.gesture.GestureOverlayView.OnGesturePerformedListener;import android.gesture.Prediction;import android.view.GestureDetector;import android.view.GestureDetector.SimpleOnGestureListener;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {GestureOverlayView gestureOverlayView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);gestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);/* * 1.找到刚才的与设定的手势文件 2. 加载那个手势文件中的所有手势 3. 匹配 识别 */// 从资源中将手势库文件加载进来:final GestureLibrary library = GestureLibraries.fromRawResource(MainActivity.this, R.raw.gestures);library.load();// 给gestureOverlayView加监听器:gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {@Overridepublic void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {// TODO Auto-generated method stub// 读出手势库中的内容,识别手势:ArrayList<Prediction> mygesture = library.recognize(gesture);Prediction prediction = mygesture.get(0); // 取出了第一个手势// 查看相似度:if(prediction.score >= 5.0){if(prediction.name.equals("exit")){finish();}else if(prediction.name.equals("next")){Toast.makeText(MainActivity.this, "播放下一首歌", 2000).show();}else if(prediction.name.equals("previous")){Toast.makeText(MainActivity.this, "播放上一首歌", 2000).show();}}else{Toast.makeText(MainActivity.this, "没有该手势", 2000).show();}}})}}
效果图:

整个手势能识别的范围:






0 0