马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.在布局文件里面添加手势识别控件:<android.gesture.GestureOverlayView
android:layout_weight="140"
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="0dip"
android:gestureStrokeType="multiple"
/>
2.在activity里面添加手势识别的代码,打对勾的是识别成功的,如果成功的话会跳转到另一个程序页面上:package com.example.gestureoverlayview_test;
import java.util.List;
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.GestureOverlayView.OnGesturePerformedListener;
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 gv;
private GestureLibrary gestureLibrary;
private Gesture gesture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GestureOverlayView) this.findViewById(R.id.gv);
//加载手势库
gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/gestures");
gestureLibrary.load();
//单笔手势
//gv.addOnGesturePerformedListener(new MyOnGesturePerformedListener());
gv.addOnGestureListener(new MyOnGestureListener()); //多笔手势
}
public void click(View v)
{
gv.clear(true);
List<Prediction> predictions = gestureLibrary.recognize(gesture); //将手势放在手势库中进行比较
if(!predictions.isEmpty())
{
Prediction prediction = predictions.get(0); //与之最相近的手势
if(prediction.score>8 && "close".equals(prediction.name)) //如果对比度大于8并且名字是close的话
{
finish(); //就关闭本页面
}else if(prediction.score > 8 && "open".equals(prediction.name))
{
Intent intent = new Intent();
intent.setClassName("com.example.video", "com.example.video.MainActivity");
startActivity(intent);
}
}else
{
Toast.makeText(getApplicationContext(), "没有匹配的手势", Toast.LENGTH_LONG).show();
}
}
private class MyOnGestureListener implements OnGestureListener //多笔手势
{
@Override
public void onGesture(GestureOverlayView arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGestureCancelled(GestureOverlayView arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent arg1) {
// TODO Auto-generated method stub
gesture = overlay.getGesture();
}
@Override
public void onGestureStarted(GestureOverlayView arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
}
}
private class MyOnGesturePerformedListener implements OnGesturePerformedListener //单笔手势
{
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
// TODO Auto-generated method stub
List<Prediction> predictions = gestureLibrary.recognize(gesture);
if(!predictions.isEmpty())
{
Prediction prediction = predictions.get(0);
if(prediction.score > 8 && "open".equals(prediction.name)) //如果是open的话,就跳转到另外一个页面
{
Intent intent = new Intent();
intent.setClassName("com.example.video", "com.example.video.MainActivity");
startActivity(intent);
}
}else
{
Toast.makeText(getApplicationContext(), "没有匹配手势", Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onDestroy() { //最后杀死进程
// TODO Auto-generated method stub
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}
}
**********点击识别*****************
*************成功后就会跳转到这个页面*************
|