android手势

来源:互联网 发布:淘宝卖家申请直播入口 编辑:程序博客网 时间:2024/04/29 19:43


<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"
    tools:context=".MainActivity" >


    <android.gesture.GestureOverlayView
        android:id="@+id/gestures"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="10">
    </android.gesture.GestureOverlayView>
<Button 
   android:id="@+id/btn"
   android:onClick="gesture"
   android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="识别手势"
        android:layout_weight="1"
   />
</LinearLayout>

package com.hbsi.csdn.gesturetest;



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


public class MainActivity extends Activity { 
private Button btn;
private GestureOverlayView gov;
private GestureLibrary libray;
private Gesture mygesture;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gov = (GestureOverlayView) findViewById(R.id.gestures); // 获取到手势的控件
libray = GestureLibraries.fromRawResource(this, R.raw.gestures); // 获取到手势库文件
gov.addOnGestureListener(new OnGestureListener() {


@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub
System.out.println("手势正在画的时候调用的方法");
}


@Override
public void onGestureCancelled(GestureOverlayView overlay,
MotionEvent event) {
System.out.println("手势取消的时候调用的方法");


}


@Override
public void onGestureEnded(GestureOverlayView overlay,
MotionEvent event) {
// TODO Auto-generated method stub
mygesture = overlay.getGesture();


}


@Override
public void onGestureStarted(GestureOverlayView overlay,
MotionEvent event) {
// TODO Auto-generated method stub
System.out.println("开始画的时候调用");
}


});


}


public void gesture(View v) {
libray.load(); // 加载手势库
ArrayList<Prediction> predictions = libray.recognize(mygesture);// 从手势库中查询匹配的内容,匹配的结果可能包括多个相似的结果,匹配度最高的结果放在最前面
Prediction prediction=predictions.get(0);//把匹配度最高的结果获取过来
if(prediction.score>=5){
if(prediction.name.equals("call")){
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:119"));
startActivity(intent);
}else if(prediction.name.equals("close")){
finish();
}else if(prediction.name.equals("Toast")){
Toast.makeText(this, "woshitosi", 0).show();
}
}else{
Toast.makeText(this, "手势不能识别", 0).show();
}
}


}
原创粉丝点击