青玄 发表于 2014-11-19 20:26:29

[原创]android学习之手势识别功能

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();
        }
       
       
}
**********点击识别*****************


*************成功后就会跳转到这个页面*************



拈花小仙 发表于 2014-11-19 21:41:27

青玄太厉害了~

青玄 发表于 2014-11-19 22:22:20

拈花小仙 发表于 2014-11-19 21:41
青玄太厉害了~

{:5_109:}小仙过奖了! 不知道小仙最近在忙什么!

青玄 发表于 2014-11-19 22:22:20

拈花小仙 发表于 2014-11-19 21:41
青玄太厉害了~

{:5_109:}小仙过奖了! 不知道小仙最近在忙什么!

拈花小仙 发表于 2014-11-19 22:39:21

青玄 发表于 2014-11-19 22:22
小仙过奖了! 不知道小仙最近在忙什么!

{:7_139:}

1012662902 发表于 2014-11-20 00:51:56

支持楼主

从0到100 发表于 2014-11-25 21:23:50

必须得顶下
页: [1]
查看完整版本: [原创]android学习之手势识别功能