• android 手势识别学习


    引自http://www.cnblogs.com/android100/p/android-hand.html    http://blog.csdn.net/jiangshide/article/details/6293017

     android的手势的识别和创建。使用到的是自带的android.gesture包,具体的例子参考的是Sample中GestureBuilder程序,利用该SDK下的GestureBuilder来生成势gestures文件下,然后我们把gestures文件拷贝到新建项目下一个新建的:/res/raw/gestures下,实现手势检测:

    main.xml文件中设置如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
     <android.gesture.GestureOverlayView
      android:id="@+id/gestures"
      android:layout_width="fill_parent"
      android:layout_height="0dip"
      android:layout_weight="1.0"
      android:gestureStrokeType="multiple"
      />
    </LinearLayout>

    其配置支字符参数为strings.xml设置如下:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, MainActivity!</string>
        <string name="app_name">手势识别2</string>
        <string name="norecohnize">不能识别该手势</string>
        <string name="nopediction">手势识别百分率太低,请重新输入</string>
    </resources>

    src下的mainactivity.jar源代码为:主要实现的功能有通过一个手势来实现拨打指定某个人的电话与再通过一个手势来实现退出的功能,

    package com.jsd.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.Prediction;
    import android.gesture.GestureOverlayView.OnGesturePerformedListener;
    import android.net.Uri;
    import android.os.Bundle;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
     private GestureLibrary libraray;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            libraray = GestureLibraries.fromRawResource(this, R.raw.gestures);//加载手势库对象
            libraray.load();//加载手势库
            GestureOverlayView overlayView = (GestureOverlayView)this.findViewById(R.id.gestures);
             
            overlayView.addOnGesturePerformedListener(new GestureListener());
        }
        
        private final class GestureListener implements OnGesturePerformedListener{
    
      @Override
      public void onGesturePerformed(GestureOverlayView overlay,
        Gesture gesture) {
       // TODO Auto-generated method stub
       ArrayList<Prediction> predictions = libraray.recognize(gesture);//识别用户输入的手势是否存在手势库中
       if(!predictions.isEmpty()){
        Prediction prediction = predictions.get(0);//得到匹配的手势
        if(prediction.score > 3){
         if("close".equals(prediction.name)){
          //关闭应用
          finish();
         }else if("phone".equals(prediction.name)){
          //指定某个人打电话
          Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:18601159149"));
          startActivity(intent);
          
         }
        }else{
         Toast.makeText(MainActivity.this, R.string.nopediction, 1).show();
        }
       }else{
        Toast.makeText(MainActivity.this, R.string.norecohnize, 1).show();
       }
      }
         
        }
        /**
         * 在这个方法中来调用其关闭
         * 关闭应用的方法有三种:
         * 1.获取其进程ID来杀死该进程:推介使用:android.process.killProcess(android.os.Process myPid());
         * 2.终止正在运行的JAVA虚拟机,从而导致程序终止:System.exist(0);
         * 3.强制关闭与该报有关的一切执行:AcitvityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);manager.restartPackage(getPackageName());<uses-permission android:name="android.permission.RESTART_PACKAGES"/>
         */
        private void onDstroy() {
      // TODO Auto-generated method stu
         android.os.Process.killProcess(android.os.Process.myPid());//当ACTIVITY被摧毁的时候我们就把应用给杀死
         
     }
    }

    代码详解:

    手势识别:

    1.什么是手势识别技术:如一个人使用一个手指在屏幕上画上某些符号来代表的说需要操作的某项业务,如画个圈代表向某个人打电话等.

    2.建立手势哭:类似于数据库,即一些手势符号的数据存储.看一个例子:sdk/samples/android-8/GestureBuilder,建立好的手势库会存在SD卡上面,默认的文件名称为:gestures.

    3.根据用户输入的手势后进行判断,如果其数据库存在相应的手势就返回出来,

    4.在res下面新建一个专门用来存放静态文件的目录raw,把手势库文件拷贝其目录文件下,当然它也会在gen目录下的R类中生成关于该文件的一个常量引用

    5.然后在main.xml中写入:

    <android.gesture.GestureOverlayView
      android:id="@+id/gestures"
      android:layout_width="fill_parent"
      android:layout_height="0dip"
      android:layout_weight="1.0"/>

    通过以上的控件,用户就会通过手指在手机上画对应符号出来,然后就需要在代码进行引用了:GestureOverlayView

    overlayView = (GestureOverlayView)this.findViewById(R.id.gestures);

    6.添加一个手势绘制完之后的监听事件:overlayView.addOnGesturePerformedListener(new GestureListener());

    7.提供一个类来对手势监听实现接口:private final class GestureListener implements

    OnGesturePerformedListener{  

    //当用户画完之后就会给用户一个参数传入其方法

     public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture){//实现接口的方法

      //下面就需要判断手势是否存在数据库中与其精度是否达到要求,这时需要加载手势库这个类:  

     ArrayList<Prediction> prediction = libraray.recognize(gesture);//识别用户输入的手势是否存在手势库中,并返回所有跟这个手势相似的手势,并且它会把相似度最高的手势放在最前面,也就是说在这个机会中的第一

    个元素相似度是最高的,现在只需要相似度最高的手势即可:   

      if(!predictions.isEmpty()){
       Prediction prediction = predictions.get(0);//得到最匹配的手势
       if(prediction.score){//判断相似度:0~10  >40%即可
         if("close".equals(prediction.name)){
          //关闭应用:1.首先获取当前进程ID,然后杀死该进程(建议使用):android.Process.killProcess(android.os.Process myPid());
    2.终止当前正在运行的Java虚拟机,导致程序终止:System.exit(0);
    3.强制关闭与该包有关联的一切执行:ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);manager.restartPackage(getPackageName());<uses-permission android:name="android.permission.RESTART_PACKAGERS"/>;这里需要注意,我们不能在这里直接使用这三种方法中的一 种,如果这样的话ACTIVITY的ONDESTORY()方法就无法调用以至于无法正常关闭,但是我们可以在这里点调用finish()方法 来让其ACTIVITY先正常关闭,然后在触发ONDESTORY()里进行调用 }else if("phone".equals(prediction.name)){ //指定一个人打电话 } }else{ Toast.makeText(MainActivity.this,R.string/nopediction,1).show; } }else{ Toast.makeText(MainActivity.this,R.string.norecognize,1).show; } } }
  • 相关阅读:
    ***mysql索引总结----mysql索引类型以及创建
    XCODE快捷键个人总结
    ios下划线变量:为什么变量前要加下划线才有用?
    ios开发之AppDelegate
    **IOS:xib文件解析(xib和storyboard的比较,一个轻量级一个重量级)
    NEON简介【转】
    NEON在Android中的使用举例【转】
    时钟频率的理解--笔记【原创】
    理解逐次逼近寄存器型ADC:与其它类型ADC的架构对比【转】
    模数转换器(ADC)的基本原理【转】
  • 原文地址:https://www.cnblogs.com/Anita9002/p/3953755.html
Copyright © 2020-2023  润新知