android识别手势

一、在布局layout中添加手势控件

<?xml version="1.0" encoding="utf-8" ?>

- < LinearLayout xmlns:android =" http://schemas.android.com/apk/res/android " android:orientation =" vertical " android:layout_width =" fill_parent " android:layout_height =" fill_parent ">
  < android.gesture.GestureOverlayView android:layout_width =" fill_parent " android:layout_height =" 0dip " android:layout_weight =" 1.0 " android:gestureStrokeType =" multiple " android:id =" @+id/gestures " />
  </ LinearLayout >

二、编码

public class MainActivity extends Activity {
    private boolean success;
    private GestureLibrary library;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //找到手势库
        library = GestureLibraries.fromRawResource(this, R.raw.gestures);
        //加载手势库
        success = library.load();
        GestureOverlayView gestureView = (GestureOverlayView)this.findViewById(R.id.gestures);//已经把手势文件放在raw文件夹下
        gestureView.addOnGesturePerformedListener(new GestureListener());

    }
   
    private final class GestureListener implements OnGesturePerformedListener{
  @Override
  public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
   if(success){
    //从手势库中查找匹配的手势,最匹配的记录会放在最前面
    ArrayList<Prediction> predictions = library.recognize(gesture);
    if(!predictions.isEmpty()){
     Prediction prediction = predictions.get(0);//最匹配的放在最前,匹配分为1至10
     Log.i("MainActivity", "score:"+ prediction.score);
     if(prediction.score>3){//匹配的级别
      if("agree".equals(prediction.name)){//匹配的名字
       android.os.Process.killProcess(android.os.Process.myPid());
      }else if("5556".equals(prediction.name)){
       Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:5556"));
       startActivity(intent);
      }
     }
    }
   }
  }
     
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值