android 编程之 手势识别

用于话手势的控件是  <android.gesture.GestureOverlayView android:gestureStrokeType="multiple"//允许多笔手势 />

1.定义一个手势工厂 存放手势图案
private GestureLibrary gestureLibrary=GestureLibraries.fromRawResource(this, R.raw.gestures);

2.加载手势工厂
 gestureLibrary.load();

3.然后监听手势控件上的手势
 (gesture).addOnGesturePerformedListener(new GestureListener());//监听手势 这个只监听单笔手势new GestureListener回调函数 定义如下


public void onGesturePerformed(GestureOverlayView arg0, Gesture arg1) 

{//第一个参数就是这里定义的接受手势的控件  第二个参数是手势画完了后的图案
   // TODO Auto-generated method stub
   ArrayList<Prediction> predictions =  gestureLibrary.recognize(arg1);//看画出来的手势图案是否和手势工厂里存放的手势图案一样 返回ArrayList<Prediction>
   Log.i("Tag","1");
   if(predictions!=null)

   {//不为空  则证明有手势图案
      Log.i("Tag","2");
      Prediction prediction = predictions.get(0);//取得栈顶的第一个最匹配的手势
      if(prediction.score>=5)

      {//判断手势的匹配度
        Log.i("Tag","3");
        Log.i("Tag",prediction.name);
        if("call".equals(prediction.name))

        {//判断是哪一种手势
           Log.i("Tag","4");
           Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:5556"));
           startActivity(intent);
        }
        else if("out".equals(prediction.name))

        {
           finish();//不是关闭应用 只是关闭activity
        }
     }
     else

     {
        Toast.makeText(getApplicationContext(), "匹配度不够", 2).show();     
     } 
   }
   else

   {
      Toast.makeText(getApplicationContext(), "没有匹配记录", 2).show();
   }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值