手势识别

这里主要介绍多笔手势识别(在文章的最后面会附上单笔手势识别的代码)


1、/res/raw/目录下导入手势库文件gesture,这个文件可以利用Android自带的GestireBuilder来绘制.

回执号的手势库会保存在/mnt/sdcard目录下。


2、main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    

    <android.gesture.GestureOverlayView 
        android:id="@+id/gov"
        android:gestureStrokeType="multiple"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
        
     <LinearLayout 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         >
         <Button 
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:onClick="ok"
             android:text="确定"
             />
         
         <Button 
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:onClick="cancel"
             android:text="取消"
             />
         
     </LinearLayout>        
</LinearLayout>



3、MainActivity

package com.njupt.gesture1;

import java.util.ArrayList;

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

public class MainActivity extends Activity {

	private GestureOverlayView gov;
	private GestureLibrary library;
	private Gesture gesture;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		gov = (GestureOverlayView) findViewById(R.id.gov);
		gov.addOnGestureListener(new MyOnGestureListener());
		library = GestureLibraries.fromRawResource(getApplicationContext(), R.raw.gestures);
		library.load();
	}

	private class MyOnGestureListener implements OnGestureListener{

		@Override
		public void onGestureStarted(GestureOverlayView overlay,
				MotionEvent event) {
			// TODO Auto-generated method stub
			
		}

		@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
			
		}
		
	}
	
	public void ok(View v){
		recognize(gesture);
		gov.clear(true);
	}
	
	public void cancel(View v){
		gov.clear(true);
	}
	
	public void recognize(Gesture gesture){
		ArrayList<Prediction> predictions = library.recognize(gesture);
		
		if(predictions.isEmpty()){
			Toast.makeText(getApplicationContext(), "手势不匹配", 1).show();
		}else{
			Prediction prediction = predictions.get(0);
			double score = prediction.score;
			if(score >= 5){
				String name = prediction.name;
				if(name.equals("call")){
					Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+5556));
					startActivity(intent);
				}else if(name.equals("close")){
					android.os.Process.killProcess(android.os.Process.myPid());
				}
			}else{
				Toast.makeText(getApplicationContext(), "匹配度太低.....", 1).show();
			}
		}
		
		
	}
	@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;
	}

}


4、AndroidManifest.xml

因为生面的程序中用到了打电话的权限,所以要在清单文件中注册打电话的权限

 <uses-permission android:name="android.permission.CALL_PHONE"/>



-------------------------------------------------以下附上但比手势识别的核心代码——————---------------------------------

 gov.addOnGesturePerformedListener(new MyOnGesturePerformedListener());只针对单笔手势


private final class MyOnGesturePerformedListener implements OnGesturePerformedListener{

		public void onGesturePerformed(GestureOverlayView overlay,
				Gesture gesture) {
			// TODO Auto-generated method stub
			//识别手势
			recognize(gesture);
		}
    	
    }




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值