gesture手势相关操作

一:保存手势到文件中:
private final File mStoreFile = new File(Environment.getExternalStorageDirectory(), "gestures");//在sdk目录生成一个gestures文件
GestureLibrary library = GestureLibraries.fromFile(mStoreFile);
GestureOverlayView overlay=(GestureOverlayView)findViewById(R.id.gestures_overlay);
        overlay.addOnGestureListener(new GestureOverlayView.OnGestureListener() {
			
			@Override
			public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
				
			}
			@Override
			public void onGestureEnded(GestureOverlayView overlay,
					MotionEvent event) {
				Gesture gesture = overlay.getGesture();
				library.addGesture("给手势取一个名字吧", mGesture);
				library.save();
				if (mGesture.getLength() < LENGTH_THRESHOLD) {
					overlay.clear(false);
				}
			}
			
			@Override
			public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
				
			}
			
			@Override
			public void onGesture(GestureOverlayView overlay, MotionEvent event) {
				
			}
		});


gesture手势相关操作


二:根据文件中保存的手势执行对应的操作
private GestureLibrary mLibrary = GestureLibraries.fromRawResource(context, R.raw.gestures);
mLibrary.load();

overlay.addOnGesturePerformedListener(new OnGesturePerformedListener() {
			@Override
			public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {
				ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
				if (predictions.size() > 0) {
					Prediction prediction = predictions.get(0);
					if(prediction.name.equals("close")){
						//执行关闭操作
					}else if(prediction.name.equals("come on")){
						//执行come on 操作
					}else if(prediction.name.equals("yes")){
						//执行yes 操作
					}
				}
			}
		});


三:手势转图片
Bitmap bitmap = gesture.toBitmap(width, height, inset, color);

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