Android手势识别应用

1、建立手势库,利用Android  samples 里边的GestureBuilder项目实现(后附源码),完成后把gestures文件拷到项目的raw文件夹下

2、应用代码:

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.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
    private boolean success;
    private GestureLibrary library;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //找到手势库
        library = GestureLibraries.fromRawResource(this, R.raw.gestures);
        //加载手势库
        success = library.load();
        GestureOverlayView gestureView = (GestureOverlayView)this.findViewById(R.id.gestures);
        gestureView.addOnGesturePerformedListener(new GestureListener());

    }
    
    private final class GestureListener implements OnGesturePerformedListener{
		@Override
		public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
			if(success){
				//从手势库中查找匹配的手势,最匹配的记录会放在最前面
				ArrayList<Prediction> predictions = library.recognize(gesture);
				if(!predictions.isEmpty()){
					Prediction prediction = predictions.get(0);
					Log.i("MainActivity", "score:"+ prediction.score);
					if(prediction.score>3){
						if("agree".equals(prediction.name)){
							android.os.Process.killProcess(android.os.Process.myPid());
						}else if("5556".equals(prediction.name)){
							Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:5556"));
							startActivity(intent);
						}
					}
				}
			}
		}
    	
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
	<android.gesture.GestureOverlayView
	    android:layout_width="fill_parent" 
	    android:layout_height="0dip"
	    android:layout_weight="1.0"
	    android:gestureStrokeType="multiple"
	    android:id="@+id/gestures"
	    />

</LinearLayout>

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.itcast.gesture"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest> 


GestureBuilder:http://pan.baidu.com/share/link?shareid=274431&uk=1796216265

项目源码:http://pan.baidu.com/share/link?shareid=274433&uk=1796216265

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值