android手势识别的实现

来源:互联网 发布:ccf工资计算c语言 编辑:程序博客网 时间:2024/05/21 09:51
1、利用手势工具绘制手势:

  

2、在eclipse中的File Explorer中的sdcard中导出gestures手势文件,如果没有显示,用UltraISO工具打开创建的sdcard导出gestures手势文件;


3、在项目中的res目录中创建raw目录,将gestures手势文件复制到该目录;


4、参考以下代码试下手势的识别:


xml中:

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


java中:

package com.asia.tmw.gesture;
import java.util.ArrayList;
import android.annotation.SuppressLint;
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.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;


@SuppressLint("ShowToast")
public class MainActivity extends Activity {


private GestureOverlayView overlayView;
private final String tag = "GesturePerformedListener";
private GestureLibrary library;
private Gesture mgesture;// 保存最终的手势


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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());


}


public void find(View v) {//xml中创建的点击按钮调用的方法
recognize(mgesture);
overlayView.clear(true);
}


private final class GestureListener implements OnGestureListener {
// 画的过程中不断触发
@Override
public void onGesture(GestureOverlayView arg0, MotionEvent arg1) {
Log.i(tag, "onGesture()");
}


@Override
public void onGestureCancelled(GestureOverlayView arg0, MotionEvent arg1) {
Log.i(tag, "onGestureCancelled()");
}


// 绘制手势结束时,取得的是最终的手势
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent arg1) {
//Log.i(tag, "onGestureEnded()");
mgesture = overlay.getGesture();// 取得用户最后画完的手势


}


// 手势开始时
@Override
public void onGestureStarted(GestureOverlayView arg0, MotionEvent arg1) {
Log.i(tag, "onGestureStarted()");
}


}


private void recognize(Gesture gesture) {
ArrayList<Prediction> predictions = library.recognize(gesture);
if (!predictions.isEmpty()) {
Prediction prediction = predictions.get(0);
Log.i(tag, "prediction.score:" + prediction.score);
Log.i(tag, "prediction.name:" + prediction.name);
Log.i(tag, prediction.name.substring(0, 5));
if (prediction.score >= 4) {// 取得相似度值为0-10,0表示相似,1表示相似度10%,10表示相似度100%
if (prediction.name.equals("phonecall")) {
Intent intent = new Intent(Intent.ACTION_CALL,
Uri.parse("18008995508"));
startActivity(intent);
} else if (prediction.name.equals("close")) {
finish();// 关闭窗口,会触发onDestroy()方法
} else if (prediction.name.substring(0, 4).equals("left")) {
Toast.makeText(getApplicationContext(), "向左滑动", 1).show();
} else if (prediction.name.substring(0, 5).equals("right")) {
Toast.makeText(getApplicationContext(), "向右滑动", 1).show();
} else {
Toast.makeText(getApplicationContext(), R.string.otners, 1).show();
}
} else {
Toast.makeText(getApplicationContext(), R.string.low, 1).show();
}
} else {
Toast.makeText(getApplicationContext(), R.string.notfind, 1).show();
}
}


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


@Override
protected void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());// 杀死当前进程ID
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

0 0
原创粉丝点击