您的位置:首页 > 移动开发 > Android开发

Android 手势识别开发—手势自动拨号

2015-06-11 12:14 411 查看
本文Android手势识别主要开发两个功能

识别手势自动拨号

识别手势关闭程序

第一步:建立手势库

使用sdk自带例子GestureBuilder建立手势库(位置:adt-bundle-windows-x86-20140321\sdk\samples\android-15\GestureBuilder)。使用GestureBuilder之前,你需要恢复其到开发环境中,然后进行编译并部署到手机上。此时,就可以使用GestureBuilder建立手势库,生成的手势库文件在sdcard上,默认文件名称为:gestures。

具体步骤:

1. sdk目录下找到工程GestureBuilder,复制GestureBuilder到Android workspaces目录下。

2. 新建我的项目gesture,复制项目gesture目录下的如下三个文件到GestureBuilder根目录下,




3. 将GestureBuilder导入eclipse中,编译部署到模拟器上运行,建立手势库如下,







4. 建立完毕后,在sdcard目录下生成手势库文件gestures,导出文件gestures。



第二步:开发手势识别

在应用中加载手势库文件gestures,然后开发手势识别代码。

将手势库文件gestures复制到项目gesture的res/raw目录下。这时候在项目gesture的R文件中就可以看到gestures的id了。然后在布局文件中添加用于手势绘制的view:

<android.gesture.GestureOverlayView
        android:id="@+id/gestures"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gestureStrokeType="multiple" />


大多数情况下,手势都是通过一笔完成。然而有一些特别的需求就需要通过多个笔画来实现,这是可以使用gestureStrokeType属性进行设置:android:gestureStrokeType=”multiple”。



main.xml

<?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/gestures" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:gestureStrokeType="multiple" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:onClick="find"
android:text="@string/recognize" />

</LinearLayout>


MainActivity.java

package cn.itcast.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.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private GestureLibrary library;
    private Gesture mgesture;
    private GestureOverlayView overlayView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        library = GestureLibraries.fromRawResource(this, R.raw.gestures);//得到手势库
        library.load();//加载手势库

        overlayView = (GestureOverlayView) this.findViewById(R.id.gestures);
        //只针对单笔手势:overlayView.addOnGesturePerformedListener(new GesturePerformedListener());
        overlayView.addOnGestureListener(new GestureListener());//为view添加手势监听事件
    }

    public void find(View v){
        recognize(mgesture);
        overlayView.clear(true);
    }

    private final class GestureListener implements OnGestureListener{
        public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
            Log.i(TAG, "onGestureStarted()");
        }
        public void onGesture(GestureOverlayView overlay, MotionEvent event) {
            Log.i(TAG, "onGesture()");
        }
        public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
            Log.i(TAG, "onGestureEnded()");
            mgesture = overlay.getGesture();
        }
        public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
            Log.i(TAG, "onGestureCancelled()");
        }
    }

    private final class GesturePerformedListener implements OnGesturePerformedListener{
        public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
            recognize(gesture);
        }       
    }

    private void recognize(Gesture gesture) {
        //从手势库中查询匹配的内容,匹配的结果可能包括多个相似的内容,匹配度高的结果放在最前面。
        ArrayList<Prediction> predictions = library.recognize(gesture);
        if(!predictions.isEmpty()){
            Prediction prediction = predictions.get(0);
            if(prediction.score >= 6){//匹配率大于60%
                if("call".equals(prediction.name)){
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:1350505050"));
                    startActivity(intent);
                }else if("close".equals(prediction.name)){
                    finish();//关闭Activity
                }
            }else{
                Toast.makeText(getApplicationContext(), R.string.low, 1).show();
            }
        }else{
            Toast.makeText(getApplicationContext(), R.string.notfind, 1).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        android.os.Process.killProcess(android.os.Process.myPid());//关闭应用
    }

}


编译部署到模拟器中运行,当输入手势对勾,程序主动自动关闭退出。当应用不再使用时,通常需要关闭应用,可以使用一下三种方法关闭android应用,

获取当前进行的id,然后杀死进程(建议使用)

android.os.Process.killProcess(android.os.Process.myPid());//关闭应用

终止当前正在运行的java虚拟机,导致程序终止

System.exit(0);

强制关闭与该包有关联的一切执行

ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

manager.restartPackage(getPackageName());

< uses-permisson android:name=”android.permission.RESTART_PACKAGESS”/>

除此之外,当画出手势z时,自动拨号






利用Android手势识别可以开发如下功能,比如说:

手机解锁密码

应用快捷方式(拨号、打开程序等)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: