Android学习笔记_28_手势识别

一、准备手势库:

  使用SDK自带例子GestureBuilder建立手势库(位置:android-sdk-windows\samples\android-10\GestureBuilder)。使用GestureBuilder之前,你需要恢复其到开发环境,将其他正确项目下的".classpath",".project"和"project.properties"三个文件拷贝到GestureBuilder项目下,导入到开发环境,然后进行编绎并部署到手机上。此时,就可以使用GestureBuilder建立手势库,生成的手势库文件在SCDard上,默认文件名称为:gestures。

二、开发:

  1、配置文件:

  在配置文件中加入拨打电话权限,后面用到打电话。

<!-- 拨打电话  -->
    <uses-permission android:name="android.permission.CALL_PHONE"/>

  布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <!--
        gestureStrokeType:可以设置单笔识别或多笔识别
        layout_weight:优先级别默认为0,最高为0,次之1...。显示界面时,先测量按钮的高度,然后再用窗口高度减去按钮高度,所得的高度设置到手势界面
    -->
    <android.gesture.GestureOverlayView
        android:id="@+id/gestures"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/btnCheck"
        android:layout_weight="1"
        android:gestureStrokeType="multiple" />
   <Button
        android:id="@+id/btnCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_weight="0"
        android:onClick="match"
        android:text="@string/check" />
</RelativeLayout>

  2、后台代码:

package com.example.gesture;

import java.util.List;

import android.R.anim;
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.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    
    private static final String TAG="Gesture";
    private GestureOverlayView gestureOverlayView;
    private GestureLibrary mLibrary;
    private boolean state;
    private Gesture gesture;//用户最终画完手势

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        state = mLibrary.load();//加载手势库
        Log.i(TAG, state+"");
        gestureOverlayView = (GestureOverlayView)this.findViewById(R.id.gestures);
        //当用户完成一次Gesture绘制后,系统将自动调用Listener对象的onGesturePerformed()方法,只支持单笔手势
        //gestureOverlayView.addOnGesturePerformedListener(new GestureListener());
        //可以监听单笔和多笔识别
        gestureOverlayView.addOnGestureListener(new MyGestureListener());
    }
    
    public void match(View v){
        matchGesture(gesture);
        gestureOverlayView.clear(true);
    }
    //单多笔监视
    private final class MyGestureListener implements OnGestureListener{
        @Override
        public void onGesture(GestureOverlayView overlay, MotionEvent event) {
             Log.i(TAG, "onGesture() ... ");
        }
        @Override
        public void onGestureCancelled(GestureOverlayView overlay,
                MotionEvent event) {
            Log.i(TAG, "onGestureCancelled() ... ");
        }
        @Override
        public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
            Log.i(TAG, "onGestureEnded() ... ");
            gesture=overlay.getGesture();
        }
        @Override
        public void onGestureStarted(GestureOverlayView overlay,
                MotionEvent event) {
            Log.i(TAG, "onGestureStarted() ... ");
        }
    }
    
    //单笔手势监听类
    private final class GestureListener implements GestureOverlayView.OnGesturePerformedListener{
        @Override
        public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
            if(state){
                matchGesture(gesture);
            }
        }
    }
    private void matchGesture(Gesture gesture) {
        //123:对号,258:X,369:李,456:Z
        //从手势库中查询匹配的内容,匹配的结果可能包括多个相似的结果,匹配度高的结果放在最前面
        List<Prediction> predictions = mLibrary.recognize(gesture);
        if(!predictions.isEmpty()){
            Prediction prediction = predictions.get(0);
            //prediction的score属性代表了与手势的相似程度
            //prediction的name代表手势对应的名称
            if(prediction.score > 6){
                if("123".equals(prediction.name)){
                    Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:159016"));
                    startActivity(intent);
                }else if ("456".equals(prediction.name)) {
                    finish();//关闭activity,会触发onDestroy方法,进而关闭应用
                }else if ("369".equals(prediction.name)) {
                    Toast.makeText(getApplicationContext(), "结果:李", Toast.LENGTH_LONG).show();
                }else if ("258".equals(prediction.name)) {
                    Toast.makeText(getApplicationContext(), "结果:X", Toast.LENGTH_LONG).show();
                }
            }else {
                Toast.makeText(getApplicationContext(), R.string.notfull, Toast.LENGTH_LONG).show();
            }
        }else{
            Toast.makeText(getApplicationContext(), R.string.notfind, Toast.LENGTH_LONG).show();
        }
    }
    @Override
    protected void onDestroy() {
        //杀掉进程
        super.onDestroy();
        android.os.Process.killProcess(android.os.Process.myPid());//关闭应用
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

posted @ 2013-12-08 14:19  若 ♂ 只如初见  阅读(304)  评论(0编辑  收藏  举报