手势(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)是触摸平台的 ...
随机推荐
- 很好用的一个类:TJvAppXMLFileStorage
以前写软件的时候,在处理软件配置信息的时候,总是要写很多重复的代码,把配置信息写到Ini文件当中.昨天到网上查了一下,发现JVCL中提供了一个非常好用的类TJvAppXMLFileStorage,可以 ...
- c/c++ 直接使用动态库 dlopen
c/c++ 直接使用动态库 dlopen 把各个版本编译成动态库,××.so ,提供统一的接口进行调用.这里使用的工具是dlxx系列函数 dlopen void *dlopen(const char ...
- 浅解ARC中的 __bridge、__bridge_retained和__bridge_transfer
文章来源:http://www.outflush.com/2015/03/introduction-of-arc-bridge-type-transfer/ 在对 bridge 相关的修饰符解说前.首 ...
- 联系人数据库设计之AbstractContactsProvider
个人见解,欢迎交流. 联系人数据库设计,源代码下载请自行去android官网下载. package com.android.providers.contacts; import android.con ...
- 与众不同 windows phone (21) - Device(设备)之摄像头(拍摄照片, 录制视频)
原文:与众不同 windows phone (21) - Device(设备)之摄像头(拍摄照片, 录制视频) [索引页][源码下载] 与众不同 windows phone (21) - Device ...
- freemark换行输出
<!--附件图片--> <#if attatList? exists> <#if (attatList?size>0)> ...
- 拿到阿里,网易游戏,腾讯,smartx的offer的过程 (转)
前言 从今年的3月14日阿里的电话面试开始,到现在4月16日在西安悦豪酒店进行的腾讯HR面到现在一个多月了,中间先后收到了阿里,网易游戏,腾讯和smartx的offer,今天早晨刚刚接到了腾讯HR的电 ...
- ipv6加英文的中括号访问
加英文的中括号就可以,如[2001:4998:c:e33::1004],我发现这是yahoo首页.但并不是所有IPv6网站都可以通过IPv6地址访问,跟IPv4一样,网站服务器端可以只绑定域名,不接受 ...
- adm下载器
netdisk;5.2.7;PC;PC-Windows;6.2.9200;WindowsBaiduYunGuanJia
- GOJ1150(矩阵快速幂)
sum Time Limit: 1000ms Problem Description: 给定a和n,计算a+aa+aaa+aaaa+...+a...a(n个a) 的和. Input: 测试数据有多组, ...