鱼C论坛

 找回密码
 立即注册
查看: 2424|回复: 6

[原创]android学习之手势识别功能

[复制链接]
发表于 2014-11-19 20:26:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.在布局文件里面添加手势识别控件:
  1. <android.gesture.GestureOverlayView
  2.         android:layout_weight="140"
  3.         android:id="@+id/gv"
  4.         android:layout_width="match_parent"
  5.         android:layout_height="0dip"
  6.         android:gestureStrokeType="multiple"
  7.         

  8.         />
复制代码
2.在activity里面添加手势识别的代码,打对勾的是识别成功的,如果成功的话会跳转到另一个程序页面上:
  1. package com.example.gestureoverlayview_test;

  2. import java.util.List;

  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.gesture.Gesture;
  7. import android.gesture.GestureLibraries;
  8. import android.gesture.GestureLibrary;
  9. import android.gesture.GestureOverlayView;
  10. import android.gesture.GestureOverlayView.OnGestureListener;
  11. import android.gesture.GestureOverlayView.OnGesturePerformedListener;
  12. import android.gesture.Prediction;
  13. import android.view.Menu;
  14. import android.view.MotionEvent;
  15. import android.view.View;
  16. import android.widget.Toast;

  17. public class MainActivity extends Activity {

  18.         private GestureOverlayView gv;
  19.         private GestureLibrary gestureLibrary;
  20.         private Gesture gesture;
  21.        
  22.         @Override
  23.         protected void onCreate(Bundle savedInstanceState) {
  24.                 super.onCreate(savedInstanceState);
  25.                 setContentView(R.layout.activity_main);
  26.                
  27.                 gv = (GestureOverlayView) this.findViewById(R.id.gv);
  28.                
  29.                 //加载手势库
  30.                 gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/gestures");
  31.                 gestureLibrary.load();
  32.                 //单笔手势
  33.                 //gv.addOnGesturePerformedListener(new MyOnGesturePerformedListener());  
  34.                 gv.addOnGestureListener(new MyOnGestureListener());   //多笔手势
  35.         }

  36.         public void click(View v)
  37.         {
  38.                 gv.clear(true);
  39.                 List<Prediction> predictions = gestureLibrary.recognize(gesture);  //将手势放在手势库中进行比较
  40.                 if(!predictions.isEmpty())
  41.                 {
  42.                         Prediction prediction = predictions.get(0);   //与之最相近的手势
  43.                         if(prediction.score>8 && "close".equals(prediction.name))   //如果对比度大于8并且名字是close的话
  44.                         {
  45.                                 finish();  //就关闭本页面
  46.                         }else if(prediction.score > 8 && "open".equals(prediction.name))
  47.                         {
  48.                                 Intent intent = new Intent();
  49.                                 intent.setClassName("com.example.video", "com.example.video.MainActivity");
  50.                                 startActivity(intent);
  51.                         }
  52.                 }else
  53.                 {
  54.                         Toast.makeText(getApplicationContext(), "没有匹配的手势", Toast.LENGTH_LONG).show();
  55.                 }
  56.         }
  57.        
  58.         private class MyOnGestureListener implements OnGestureListener   //多笔手势
  59.         {

  60.                 @Override
  61.                 public void onGesture(GestureOverlayView arg0, MotionEvent arg1) {
  62.                         // TODO Auto-generated method stub
  63.                        
  64.                 }

  65.                 @Override
  66.                 public void onGestureCancelled(GestureOverlayView arg0, MotionEvent arg1) {
  67.                         // TODO Auto-generated method stub
  68.                        
  69.                 }

  70.                 @Override
  71.                 public void onGestureEnded(GestureOverlayView overlay, MotionEvent arg1) {
  72.                         // TODO Auto-generated method stub
  73.                         gesture = overlay.getGesture();
  74.                 }

  75.                 @Override
  76.                 public void onGestureStarted(GestureOverlayView arg0, MotionEvent arg1) {
  77.                         // TODO Auto-generated method stub
  78.                        
  79.                 }
  80.                
  81.         }

  82.         private class MyOnGesturePerformedListener implements OnGesturePerformedListener  //单笔手势
  83.         {

  84.                 @Override
  85.                 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
  86.                         // TODO Auto-generated method stub
  87.                         List<Prediction> predictions = gestureLibrary.recognize(gesture);
  88.                         if(!predictions.isEmpty())
  89.                         {
  90.                                 Prediction prediction = predictions.get(0);
  91.                                 if(prediction.score > 8 && "open".equals(prediction.name))  //如果是open的话,就跳转到另外一个页面
  92.                                 {
  93.                                         Intent intent = new Intent();
  94.                                         intent.setClassName("com.example.video", "com.example.video.MainActivity");
  95.                                         startActivity(intent);
  96.                                 }
  97.                         }else
  98.                         {
  99.                                 Toast.makeText(getApplicationContext(), "没有匹配手势", Toast.LENGTH_LONG).show();
  100.                         }
  101.                 }
  102.                
  103.         }
  104.        
  105.         @Override
  106.         protected void onDestroy() {   //最后杀死进程
  107.                 // TODO Auto-generated method stub
  108.                 android.os.Process.killProcess(android.os.Process.myPid());
  109.                 super.onDestroy();
  110.         }
  111.        
  112.        
  113. }
复制代码
**********点击识别*****************
QQ截图20141119200902.png

*************成功后就会跳转到这个页面*************
QQ截图20141119202507.png


评分

参与人数 1荣誉 +2 鱼币 +3 贡献 +2 收起 理由
拈花小仙 + 2 + 3 + 2 感谢楼主无私奉献!

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-11-19 21:41:27 | 显示全部楼层
青玄太厉害了~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-19 22:22:20 | 显示全部楼层

  小仙过奖了! 不知道小仙最近在忙什么!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-19 22:22:20 | 显示全部楼层

  小仙过奖了! 不知道小仙最近在忙什么!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-19 22:39:21 | 显示全部楼层
青玄 发表于 2014-11-19 22:22
小仙过奖了! 不知道小仙最近在忙什么!

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-20 00:51:56 | 显示全部楼层
支持楼主
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-25 21:23:50 | 显示全部楼层
必须得顶下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-28 02:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表