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);
      }
     }
    }
   }
  }
     
    }
}

 

你可能感兴趣的:(android识别手势)