在Android应用中,很多地方都用到了手势识别技术,比如说手机屏幕解锁、支付宝启动解锁。下面是关于手势的一些知识。
一、创建手势
在Android sdk中,自带有一个用于创建手势的应用,目录为 ...\android-sdk-windows\samples\android-10\GestureBuilder. 我们可以将该项目拷贝到workspace下,在从其他Android项目复制.classpath,.project, project.properties文件到GestureBuilder目录下,Eclipse就可以成功的导入该项目了,然后就可以将该项目发布到Android手机上。当我们通过该应用创建一些手势后,会在SDCard中创建一个手势库文件gestures。
二、单笔手势识别
在上面那个应用中,我们创建了一个手势库文件。现在在重新创建一个手势识别项目GestureDemo,将上面的应用产生的手势库文件gestures拷贝到res目录下(新建一个 /res/raw文件夹存放)。然后定义我们的layout
<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" >
<android.gesture.GestureOverlayView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gestureOverlayView" />
</RelativeLayout>
MainActivity.java文件
package com.lk.gesturedemo;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
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;
public class MainActivity extends Activity {
private GestureOverlayView gestureOverlayView;
private GestureLibrary gestureLibrary;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//手势库
gestureLibrary = GestureLibraries.fromRawResource(getApplicationContext(), R.raw.gestures);
gestureLibrary.load();
gestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView);
//针对单笔手势, 画完后触发的事件
gestureOverlayView.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
// 相似的手势,相似度从高到低存放在predictions中
List<Prediction> predictions = gestureLibrary.recognize(gesture);
if(predictions != null && predictions.size() > 0) {
Prediction prediction = predictions.get(0);
if(prediction.score >= 6) { //相似度大于60%
if(prediction.name.equals("right")) {
//当匹配度达到60%,并且名字为"right",则进行拨号动作
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel: 5556"));
startActivity(intent);
} else {
finish(); //关闭该应用
}
} else {
Toast.makeText(getApplicationContext(), R.string.low_recognize, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), R.string.un_recognize, Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onDestroy() {
// 获取当前线程进程,然后杀死该进程
//需要权限 android.permission.RESTART_PACKAGES
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}
}
三、多笔手势识别
layout
<LinearLayout 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:orientation="vertical"
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" >
<!-- android:gestureStrokeType="multiple"表示可输入多笔手势 -->
<android.gesture.GestureOverlayView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/gestureOverlayView"
android:gestureStrokeType="multiple" />
<Button
android:id="@+id/recognize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="recognizeGesture"
android:text="@string/recognize" />
</LinearLayout>
MainActivity.java
package com.lk.gesturedemo;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
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;
public class MainActivity extends Activity {
private GestureOverlayView gestureOverlayView;
private GestureLibrary gestureLibrary;
private Gesture gesture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//手势库
gestureLibrary = GestureLibraries.fromRawResource(getApplicationContext(), R.raw.gestures);
gestureLibrary.load();
gestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView);
//针对单笔手势, 画完后触发的事件
// gestureOverlayView.addOnGesturePerformedListener(new GesturePerformedListener());
//针对多笔手势
gestureOverlayView.addOnGestureListener(new GestureListener());
}
public void recognizeGesture(View view) {
recognize(gesture);
gestureOverlayView.clear(true);
}
private void recognize(Gesture gesture) {
if(gesture != null) {
// 相似的手势,相似度从高到低存放在predictions中
List<Prediction> predictions = gestureLibrary.recognize(gesture);
if(predictions != null && predictions.size() > 0) {
Prediction prediction = predictions.get(0);
if(prediction.score >= 6) { //相似度大于60%
if(prediction.name.equals("right")) {
//当匹配度达到60%,并且名字为"right",则进行拨号动作
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel: 5556"));
startActivity(intent);
} else if(prediction.name.equals("wrong")) {
finish(); //关闭该应用
}
} else {
Toast.makeText(getApplicationContext(), R.string.low_recognize, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), R.string.un_recognize, Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onDestroy() {
// 获取当前线程进程,然后杀死该进程
//需要权限 android.permission.RESTART_PACKAGES
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}
private final class GestureListener implements OnGestureListener {
//开始手势是触发
@Override
public void onGestureStarted(GestureOverlayView overlay,
MotionEvent event) {
}
//画的过程中一直触发
@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub
}
//画完每笔后触发
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
gesture = overlay.getGesture();
}
@Override
public void onGestureCancelled(GestureOverlayView overlay,
MotionEvent event) {
// TODO Auto-generated method stub
}
}
private final class GesturePerformedListener implements GestureOverlayView.OnGesturePerformedListener {
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
recognize(gesture);
}
}
}