Gesture获取屏幕手写事件并显示相应信息

 

 

private GestureLibrary gesLib;
	private GestureOverlayView gesOverlay;
	private String gesPath;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.get_gesture);

		/* 查看SDCard是否存在 */
		if (!Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())) {
			/* SD卡不存在,显示Toast信息 */
			Toast.makeText(GetGesture.this, "SD卡不存在!程序无法运行", Toast.LENGTH_LONG)
					.show();
			finish();
		}

		else {
			/* 取得GestureLibrary的文件路径 */
			gesPath = new File(Environment.getExternalStorageDirectory(),
					"gestures").getAbsolutePath();

			File file = new File(gesPath);

			if (!file.exists()) {
				/* gestures文件不存在,显示Toast信息 */
				Toast.makeText(GetGesture.this, "gestures文件不存在!程序无法运行",
						Toast.LENGTH_LONG).show();
				finish();
			}

			else {
				/* 初始化GestureLibrary */
				gesLib = GestureLibraries.fromFile(gesPath);
				if (!gesLib.load()) {
					/* 读取失败,显示Toast信息 */
					Toast.makeText(GetGesture.this, "gestures文件读取失败!程序无法运行",
							Toast.LENGTH_LONG).show();
					finish();
				} else {
					/* GestureOverlayView初始化 */
					gesOverlay = (GestureOverlayView) findViewById(R.id.myGestures1);
					gesOverlay.addOnGesturePerformedListener(new MyListener(
							this));
				}
			}
		}

	}

	/* 告定义OnGesturePerformedListener */
	public class MyListener implements OnGesturePerformedListener {
		private Context context;

		public MyListener(Context context) {
			this.context = context;
		}

		public void onGesturePerformed(GestureOverlayView overlay,
				Gesture gesture) {
			/* 手势比对 */
			ArrayList<Prediction> predictions = gesLib.recognize(gesture);
			if (predictions.size() > 0) {
				/* 取最接近的手势 */
				Prediction prediction = predictions.get(0);
				/* 取得预测值吹少己于1.0 */
				if (prediction.score > 1.0) {
					/* prediction.name?为?串预测结果 */
					Toast.makeText(this.context, prediction.name,
							Toast.LENGTH_SHORT).show();
				} else {
					/* 比对不到,显示Toast */
					Toast.makeText(this.context, "找不到匹配的Gesture!",
							Toast.LENGTH_SHORT).show();
				}
			} else {
				/* 比对不到,显示Toast */
				Toast.makeText(this.context, "找不到匹配的Gesture!",
						Toast.LENGTH_SHORT).show();
			}
		}
	}
 

<?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">
	<TextView
		android:id="@+id/myTextView1"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="请在屏幕上画出Gesture"
		android:textSize="18sp" />
	<android.gesture.GestureOverlayView
		android:id="@+id/myGestures1"
		android:layout_width="fill_parent"
		android:layout_height="0dip"
		android:layout_weight="1.0"
		android:gestureStrokeType="multiple" />
</LinearLayout>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值