手势(Gesture)的增加和识别
Android除了提供手势检测之外,还允许把用户手势添加到指定文件中,以备以后使用,当用户再次画出该手势时,系统可识别该手势。Android使用GestureLibrary代表手势库,提供GestureLibraries工具类来创建手势库。GestureLibraries提供如下4个静态方法从不同位置加载手势库:
一旦程序中获得了GestureLibrary对象后,就可以用如下方法来添加、识别手势:
另外Android还提供了一个手势编辑组件:GestureOverlayView,用户可以在上面绘制手势。然后对它绑定事件监听器,响应手势事件开始、结束、完成、取消等事件。下面通过一个简单实例来演示手势的添加以及识别自定义手势,代码如下:
AddGestureActiv:
package com.lovo.activity; import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView; import com.lovo.addgesture.R; public class AddGestureActivity extends Activity {
private GestureOverlayView gestureView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取手势编辑视图
gestureView = (GestureOverlayView) findViewById(R.id.main_gesture);
// 设置手势绘制的颜色
gestureView.setGestureColor(Color.RED);
// 设置手势的绘制宽度
gestureView.setGestureStrokeWidth(4);
// 为 gesture的手势完成事件绑定事件监听器
gestureView.addOnGesturePerformedListener(new OnGesturePerformedListener() { @Override
public void onGesturePerformed(GestureOverlayView overlay,
final Gesture gesture) {
// 加载save.xml界面布局代表的视图
View saveDialog = getLayoutInflater().inflate(R.layout.save, null);
// 获取saveDialog里的show组件
ImageView imageView = (ImageView) saveDialog.findViewById(R.id.save_iv_show);
// 获取saveDialog里的gesture_name组件
final EditText gestureName = (EditText) saveDialog
.findViewById(R.id.save_et_gesture_name);
// 根据Gesture包含的手势创建一个位图
Bitmap bitmap = gesture.toBitmap(128, 128, 10,0xFFFF0000);
imageView.setImageBitmap(bitmap);
// 使用对话框显示saveDialog组件
new AlertDialog.Builder(AddGestureActivity.this)
.setView(saveDialog)
.setPositiveButton("保存",
new android.content.DialogInterface.OnClickListener() { @Override
public void onClick(
DialogInterface dialog,int which) {
// 获取指定文件对应的手势库
GestureLibrary gestureLib = GestureLibraries
.fromFile(AddGestureActivity.this
.getFilesDir()+ "/gestures");
// 添加手势
gestureLib.addGesture(gestureName.getText().toString(),gesture);
// 保存手势库
boolean isSave = gestureLib.save();
String result = isSave ? "保存成功": "保存失败";
Log.i("isSave", result);
}
}).setNegativeButton("取消", null).show();
}
});
Button turnBtn = (Button) findViewById(R.id.main_btn_turn);
turnBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent intent = new Intent(AddGestureActivity.this,
RecogniseGestureActivity.class);
startActivity(intent);
}
});
}
}
Add布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请在下面屏幕上绘制手势" /> <Button
android:id="@+id/main_btn_turn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="跳转到手势识别界面" />
</LinearLayout>
<!-- 使用手势绘制组件 --> <android.gesture.GestureOverlayView
android:id="@+id/main_gesture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gestureStrokeType="multiple" /> </LinearLayout>
识别手势的Activity:
package com.lovo.activity; import java.util.ArrayList; import android.app.Activity;
import android.app.AlertDialog;
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.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast; import com.lovo.addgesture.R; public class RecogniseGestureActivity extends Activity {
// 定义手势编辑组件
private GestureOverlayView gestureView;
// 记录手机上已有的手势库
GestureLibrary gestureLibrary; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recognise);
// 读取上一个程序所创建的手势库
gestureLibrary = GestureLibraries.fromFile(this.getFilesDir()
+ "/gestures");
if (gestureLibrary.load()) {
Toast.makeText(this, "手势文件装载成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "手势文件装载失败!", Toast.LENGTH_SHORT).show();
}
// 获取手势编辑组件
gestureView = (GestureOverlayView) findViewById(R.id.recognise_gesture);
// 为手势编辑组件绑定事件监听器
gestureView
.addOnGesturePerformedListener(new OnGesturePerformedListener() { @Override
public void onGesturePerformed(GestureOverlayView overlay,
Gesture gesture) {
// 识别用户刚刚所绘制的手势
ArrayList<Prediction> predictions = gestureLibrary
.recognize(gesture);
ArrayList<String> result = new ArrayList<String>();
// 遍历所有找到的Prediction对象
for (Prediction pred : predictions) {
// 只有相似度大于2.0的手势才会被输出
if (pred.score > 2.0) {
result.add("与手势【" + pred.name + "】的相似度为"
+ pred.score);
}
}
if (result.size() > 0) {
ArrayAdapter adapter = new ArrayAdapter(
RecogniseGestureActivity.this,
android.R.layout.simple_dropdown_item_1line,
result.toArray());
// 使用一个带List的对话框来显示所有匹配的手势
new AlertDialog.Builder(
RecogniseGestureActivity.this)
.setAdapter(adapter, null)
.setPositiveButton("保存", null).show();
} else {
Toast.makeText(RecogniseGestureActivity.this,
"无法找到匹配的手势!", Toast.LENGTH_SHORT).show();
} }
});
Button turnBtn = (Button) findViewById(R.id.recognise_btn_turn);
turnBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
finish();
}
});
}
}
识别手势的布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请在下面屏幕上绘制手势" /> <Button
android:id="@+id/recognise_btn_turn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="返回到增加手势界面" />
</LinearLayout>
<!-- 使用手势绘制组件 --> <android.gesture.GestureOverlayView
android:id="@+id/recognise_gesture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gestureStrokeType="multiple" /> </LinearLayout>
手势(Gesture)的增加和识别的更多相关文章
- android学习笔记52——手势Gesture,增加手势、识别手势
手势Gesture,增加手势 android除了提供了手势检测之外,还允许应用程序把用户手势(多个持续的触摸事件在屏幕上形成特定的形状)添加到指定文件中,以备以后使用 如果程序需要,当用户下次再次画出 ...
- android学习笔记51——SQLite 手势Gesture
手势Gesture 所谓手势,是指用户手指或触摸笔在触摸屏幕上的连续触碰行为. Androi对两种手势行为都提供了支持: 1.对于第一种手势而言,android提供了手势检测,并为手势检测提供了相应的 ...
- 0128——手势Gesture
UIGestureRecognizer: 1.locationinView 获取手势在某个视图里面的坐标位置 2.delegate监听手势的行为 3.state状态 开始:UIGestureRecog ...
- 【转】 iOS开发之手势gesture详解
原文:http://www.cnblogs.com/salam/archive/2013/04/30/iOS_gesture.html 前言 在iOS中,你可以使用系统内置的手势识别 (Gesture ...
- iOS开发之手势gesture详解(一)
前言 在iOS中,你可以使用系统内置的手势识别(GestureRecognizer),也可以创建自己的手势.GestureRecognizer将低级别的转换为高级别的执行行为,是你绑定到view的对象 ...
- iOS开发之手势gesture详解(二)
与其他用户界面控件交互 UIControl子类会覆盖parentView的gesture.例如当用户点击UIButton时,UIButton会接受触摸事件,它的parentView不会接收到.这仅适用 ...
- eoLinker 新功能发布,增加了识别代码注释自动生成文档功能
产品地址:https://www.eolinker.com开源代码:https://www.eolinker.com/#/os/download在线生成代码注释工具:http://tool.eolin ...
- opencv编译:opencv 3.4.1 编译 contrib模块,增加人脸识别
start cmake-gui select the opencv source code folder and the folder where binaries will be built (th ...
- 手势识别(一)--手势基本概念和ChaLearn Gesture Challenge
以下转自: http://blog.csdn.net/qq1175421841/article/details/50312565 像点击(clicks)是GUI平台的核心,轻点(taps)是触摸平台的 ...
随机推荐
- hao947 : Mybatis resultMap配置插入和主键自增返回 : 好947
映射配置文件 好947 <!-- type:映射实体类的数据类型 id:resultMap的唯一标识 --> <resultMap type="person" ...
- C语言常用的宏
01: 防止一个头文件被重复包含 #ifndef COMDEF_H #define COMDEF_H //头文件内容 #endif 02: 重新定义一些类型,防止由于各种平台和编译器的不同,而产生的类 ...
- Windbg 32位版本和64位版本的选择
习惯了Vsiual Studio的兄弟们可能会因为先入为主的原因以为所有的调试器都应该像它那样,其实不然,当你安装Debugging Tools for Windows的时候,你将发现有两个系列的工具 ...
- BAD packet signature 18245 错误解决
1.错误信息 2014-7-15 2:46:38 org.apache.jk.common.MsgAjp processHeader 严重: BAD packet signature 18245 20 ...
- [Android学习笔记]SeekBar的使用
一.SeekBar滑动条的使用 xml声明: <SeekBar android:id="@+id/seekbar" android:layout_width="20 ...
- 编程算法 - 萨鲁曼的军队(Saruman's Army) 代码(C)
萨鲁曼的军队(Saruman's Army) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 直线上有N个点, 每个点, 其距离为R以内的区域里 ...
- linux LVS DR模式配置
拓扑图: 测试环境:CentOS 6.5 X86 64位 配置步骤: 1. 安装测试环境 [root@UCS-1 ~]# yum -y install httpd [root@UCS-1 ~]# c ...
- c#操作.mpp文件
原文地址:http://mjm13.iteye.com/blog/532404 所需设置 在工程中增加引用Microsoft Project 11.0 Object Library,该引用在co ...
- hdu3006(状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3006 题意:给定一些集合,求这些集合所能组合的集合个数. 分析:这题就是用一个二进制数保存一个集合的元 ...
- hdu2639(背包求第k优解)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:给出一行价值,一行体积,让你在v体积的范围内找出第k大的值 分析:dp[i][j][k]表 ...