Android手势操作

  1 xml文件
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     xmlns:tools="http://schemas.android.com/tools"
  4     android:layout_width="match_parent"
  5     android:orientation="vertical"
  6     android:layout_height="match_parent"
  7     tools:context=".MainActivity" >
  8     <android.gesture.GestureOverlayView
  9         android:id="@+id/gestureOverlayView1"
 10         android:layout_width="match_parent"
 11         android:layout_height="0dp"
 12         android:layout_weight="1"
 13         android:gestureStrokeType="multiple" >
 14     </android.gesture.GestureOverlayView>
 15     <Button 
 16         android:layout_height="wrap_content"
 17         android:layout_width="wrap_content"
 18         android:layout_gravity="center_horizontal"
 19         android:id="@+id/Button"
 20         android:layout_weight="0"
 21         android:text="识别"/>
 22 </LinearLayout>
 23 
 24 注意:
 25     android_weight=0 表示是首先计算该控件的的高度或宽度,然后计算android_weight="1"控件的 高度,一般他是和 android:layout_height="0dp"结合使用的,否则是不会出来效果
 26 
 27 
 28 
 29 public class MainActivity extends Activity {
 30     private GestureLibrary library;
 31     private Gesture gestures;
 32     protected void onCreate(Bundle savedInstanceState) {
 33         super.onCreate(savedInstanceState);
 34         setContentView(R.layout.main);
 35         GestureOverlayView gesture=(GestureOverlayView) this.findViewById(R.id.gestureOverlayView1);
 36         library=GestureLibraries.fromRawResource(this, R.raw.gestures);//得到手势文件的类文件
 37         library.load();//加载手势库
 38         
 39         /**
 40          * 为手势添加事件监听
 41          */
 42         gesture.addOnGestureListener(new OnGestureListener(){
 43             public void onGesture(GestureOverlayView overlay, MotionEvent event)
 44             {
 45                 // TODO Auto-generated method stub
 46             }
 47             @Override
 48             public void onGestureCancelled(GestureOverlayView overlay,MotionEvent event) 
 49             {
 50                 // TODO Auto-generated method stub
 51             }
 52             @Override
 53             public void onGestureEnded(GestureOverlayView overlay,MotionEvent event) 
 54             {
 55                 gestures=overlay.getGesture();//得到一个手势
 56             }
 57             @Override
 58             public void onGestureStarted(GestureOverlayView overlay,MotionEvent event)
 59             {
 60                 // TODO Auto-generated method stub
 61             }
 62         });
 63         
 64         this.findViewById(R.id.Button).setOnClickListener(new OnClickListener()
 65         {
 66             public void onClick(View v)
 67             {
 68                 find(gestures);
 69             }
 70         });
 71     }
 72 
 73     public void find(Gesture gesture)
 74     {
 75         ArrayList<Prediction> predictions=library.recognize(gesture);//得到手势库中的一个集合
 76         if(predictions!=null)
 77         {
 78             Prediction prediction= predictions.get(0);//得到一个最匹配的一项,手势匹配默认会把最匹配的一项放在最前面
 79             /**
 80              * 判断匹配的度数是否大于60%,prediction.score的值在大于0小于10的,如果是4的话就是匹配值是40%的意思
 81              */
 82             if(prediction.score>5)
 83             {
 84                 if("close".equals(prediction.name))
 85                 {
 86                     finish();//此方法只能那个关闭Activity,并不能杀死进程,所以要在OnDestory()方法中杀死进程
 87                 }
 88                 else if("call".equals(prediction.name))
 89                 {
 90                     Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:15824935047"));
 91                     startActivity(intent);
 92                 }
 93             }
 94             else
 95             {
 96                 Toast.makeText(this, "匹配值太低", Toast.LENGTH_LONG).show();
 97             }
 98         }else
 99         {
100             Toast.makeText(this, "匹配不成功", Toast.LENGTH_LONG).show();
101         }
102     }
103     
104     @Override
105     protected void onDestroy() {
106         Process.killProcess(Process.myPid());//杀死当前进程
107         super.onDestroy();
108     }
109 
110     @Override
111     public boolean onCreateOptionsMenu(Menu menu) {
112         // Inflate the menu; this adds items to the action bar if it is present.
113         getMenuInflater().inflate(R.menu.main, menu);
114         return true;
115     }
116 
117 }

 

posted on 2013-07-18 18:51  I love Android  阅读(334)  评论(0编辑  收藏  举报