Android 手势识别详解
来源:互联网 发布:魔王神官2知轩下载 编辑:程序博客网 时间:2024/05/16 08:25
第一步:写布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.gesture.GestureOverlayView
android:id="@+id/gov_gesture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gestureStrokeType="multiple"
></android.gesture.GestureOverlayView>
<Button
android:id="@+id/bt_yesid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定"
android:onClick="clickyes"
/>
<Button
android:id="@+id/bt_noid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"
android:onClick="clickno"
/>
</LinearLayout>
第二步:在res资源文件目录下建一个raw文件夹来存放手势库文件
第三步:写Activity
package com.geek.gesture;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
GestureOverlayView gov_gesture;
Gesture gesture;
GestureLibrary library;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
// TODO Auto-generated method stub
gov_gesture=(GestureOverlayView) findViewById(R.id.gov_gesture);
//加载手势库
library=GestureLibraries.fromRawResource(MainActivity.this, R.raw.gestures);
library.load();
//添加手势控件的监听事件
gov_gesture.addOnGestureListener(new MyOnGestureListtener());
}
//手势控件的监听事件类
class MyOnGestureListtener implements OnGestureListener{
@Override
public void onGestureStarted(GestureOverlayView overlay,
MotionEvent event) {
// TODO Auto-generated method stub
}
@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub
}
//手势结束
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub
//拿到手势
gesture=overlay.getGesture();
}
@Override
public void onGestureCancelled(GestureOverlayView overlay,
MotionEvent event) {
// TODO Auto-generated method stub
}
}
//确定
public void clickyes(View view){
//用拿到的手势到手势库里匹配,返回集合
ArrayList<Prediction> predictions= library.recognize(gesture);
//如果集合不为空就表示匹配到了
if(!predictions.isEmpty()){
//拿到匹配到的第一个手势
Prediction prediction=predictions.get(0);
//拿到匹配的相似度
double score=prediction.score;
//如果相似度大于等于5
if(score>=5){
//如果匹配到的手势名称是打电话
if(prediction.name.equals("打电话")){
//就跳到拨号页面
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL_BUTTON);
startActivity(intent);
}
//如果匹配到的手势名称是对
else if(prediction.name.equals("对")){
Toast.makeText(MainActivity.this, "对", 2000).show();
}
//如果匹配到的手势名称是错
else if(prediction.name.equals("错")){
Toast.makeText(MainActivity.this, "错", 2000).show();
}
}else{
//如果相似度小于5
Toast.makeText(MainActivity.this, "手势匹配度太低", 2000).show();
}
}else{
//如果集合为空就表示没有匹配到
Toast.makeText(MainActivity.this, "无法识别", 2000).show();
}
//清除手势控件里面的手势
gov_gesture.clear(true);
}
//取消
public void clickno(View view){
//清除手势控件里面的手势
gov_gesture.clear(true);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.gesture.GestureOverlayView
android:id="@+id/gov_gesture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gestureStrokeType="multiple"
></android.gesture.GestureOverlayView>
<Button
android:id="@+id/bt_yesid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定"
android:onClick="clickyes"
/>
<Button
android:id="@+id/bt_noid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"
android:onClick="clickno"
/>
</LinearLayout>
第二步:在res资源文件目录下建一个raw文件夹来存放手势库文件
第三步:写Activity
package com.geek.gesture;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
GestureOverlayView gov_gesture;
Gesture gesture;
GestureLibrary library;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
// TODO Auto-generated method stub
gov_gesture=(GestureOverlayView) findViewById(R.id.gov_gesture);
//加载手势库
library=GestureLibraries.fromRawResource(MainActivity.this, R.raw.gestures);
library.load();
//添加手势控件的监听事件
gov_gesture.addOnGestureListener(new MyOnGestureListtener());
}
//手势控件的监听事件类
class MyOnGestureListtener implements OnGestureListener{
@Override
public void onGestureStarted(GestureOverlayView overlay,
MotionEvent event) {
// TODO Auto-generated method stub
}
@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub
}
//手势结束
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub
//拿到手势
gesture=overlay.getGesture();
}
@Override
public void onGestureCancelled(GestureOverlayView overlay,
MotionEvent event) {
// TODO Auto-generated method stub
}
}
//确定
public void clickyes(View view){
//用拿到的手势到手势库里匹配,返回集合
ArrayList<Prediction> predictions= library.recognize(gesture);
//如果集合不为空就表示匹配到了
if(!predictions.isEmpty()){
//拿到匹配到的第一个手势
Prediction prediction=predictions.get(0);
//拿到匹配的相似度
double score=prediction.score;
//如果相似度大于等于5
if(score>=5){
//如果匹配到的手势名称是打电话
if(prediction.name.equals("打电话")){
//就跳到拨号页面
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL_BUTTON);
startActivity(intent);
}
//如果匹配到的手势名称是对
else if(prediction.name.equals("对")){
Toast.makeText(MainActivity.this, "对", 2000).show();
}
//如果匹配到的手势名称是错
else if(prediction.name.equals("错")){
Toast.makeText(MainActivity.this, "错", 2000).show();
}
}else{
//如果相似度小于5
Toast.makeText(MainActivity.this, "手势匹配度太低", 2000).show();
}
}else{
//如果集合为空就表示没有匹配到
Toast.makeText(MainActivity.this, "无法识别", 2000).show();
}
//清除手势控件里面的手势
gov_gesture.clear(true);
}
//取消
public void clickno(View view){
//清除手势控件里面的手势
gov_gesture.clear(true);
}
}
0 0
- Android+手势识别详解
- Android 手势识别详解
- Android手势识别详解
- Android手势识别GestureDetector详解
- 手势识别事件详解
- UIGestureRecognizer手势识别详解
- UIGestureRecognizer手势识别详解
- Android-自定义手势识别-复杂手势识别
- android手势Gesture识别
- android之手势识别
- Android 多点手势识别
- Android触摸屏手势识别
- android基础--手势识别
- android手势识别实例
- Android GestureDetector手势识别
- Android 多点手势识别
- Android 手势识别
- android之手势识别
- c# 文件处理
- Android ListView/GridView onItemClick函数参数说明及使用举例
- web maven项目配置不兼容 myeclipse interprise 和 spring 版本配置的差异
- 什么是网络安全审查制度
- 随心de面试总结
- Android 手势识别详解
- 分享12个高质量的PPT图片素材下载站
- jvisualvm 远程连接 JVM
- Memcached命令
- System.exit(0)和System.exit(1)区别
- C++和JNI的数据转换
- 查看ADT版本,安装、卸载、更新eclipse中的ADT
- 【影视世界】序列之22 【大寒桃花开】教会了我们什么?
- 基于百度地图api获取经纬度