`

Gesture建立手写笔画图案

 
阅读更多

添加权限

 

<uses-permission
		android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 

MyGesture.java

 

private Gesture ges;
	private GestureLibrary lib;
	private GestureOverlayView overlay;
	private Button button01, button02 , button03;
	private EditText et;
	private String gesPath;

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

		/* 查看SDCard是否存在 */
		if (!Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())) {
			/* SD卡不存在,显示Toast信息 */
			toast("SD卡不存在!程序无法运行");
		}
		/* 以findViewById()取得对象 */
		et = (EditText) this.findViewById(R.id.myEditText1);
		button01 = (Button) this.findViewById(R.id.myButton1);
		button02 = (Button) this.findViewById(R.id.myButton2);
		button03 = (Button) this.findViewById(R.id.myButton3);
		overlay = (GestureOverlayView) findViewById(R.id.myGestures1);

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

		/* 设置EditText的OnKeyListener */
		et.setOnKeyListener(new EditText.OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				/* 名称与手写都已设置时将新增的Button enable */
				if (ges != null && et.getText().length() != 0) {
					button01.setEnabled(true);
				} else {
					button01.setEnabled(false);
				}
				return false;
			}
		});

		/* 设定Overlay的OnGestureListener */
		overlay.addOnGestureListener(new GestureOverlayView.OnGestureListener() {
			public void onGesture(GestureOverlayView overlay, MotionEvent event) {
			}

			/* 开始画手势时将新增的Button disable,并清除Gesture */
			public void onGestureStarted(GestureOverlayView overlay,
					MotionEvent event) {
				button01.setEnabled(false);
				ges = null;
			}

			/* 手势画完时判断名称与手写是否完整建立 */
			public void onGestureEnded(GestureOverlayView overlay,
					MotionEvent event) {
				ges = overlay.getGesture();
				if (ges != null && et.getText().length() != 0) {
					button01.setEnabled(true);
				}
			}

			public void onGestureCancelled(GestureOverlayView overlay,
					MotionEvent event) {
			}
		});

		/* 设定button01的OnClickListener */
		button01.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				final String gesName = et.getText().toString();
				try {
					File file = new File(gesPath);
					lib = GestureLibraries.fromFile(gesPath);

					if (!file.exists()) {
						/* 文件不存在就直接写入 */
						lib.addGesture(gesName, ges);
						if (lib.save()) {
							/* 将设定画面数据清除 */
							et.setText("");
							button01.setEnabled(false);
							overlay.clear(true);
							/* 保存成功,显示Toast信息 */
							toast("保存成功,保存路径为:" + gesPath);
						} else {
							/* 保存失败,显示Toast信息 */
							toast("保存失败");
						}
					} else {
						/* 文件存在时因读取保存的Gesture */
						if (!lib.load()) {
							/* Library读取失败,显示Toast讯息 */
							toast("Library读取失败");
						} else {
							/* 如果Library中存在相同名称,则因将其移除再写入 */
							final Set<String> en = lib.getGestureEntries();
							if (en.contains(gesName)) {
								new AlertDialog.Builder(MyGesture.this)
										.setTitle("提醒")
										.setMessage("文件已存在,确定要覆盖吗?")
										.setNegativeButton("取消", null)
										.setPositiveButton(
												"确定",
												new DialogInterface.OnClickListener() {
													public void onClick(
															DialogInterface dialoginterface,
															int i) {
														ArrayList<Gesture> al = lib
																.getGestures(gesName);
														for (int t = 0; t < al
																.size(); t++) {
															lib.removeGesture(
																	gesName,
																	al.get(t));
														}
														lib.addGesture(gesName,
																ges);
														if (lib.save()) {
															/* 将设定画面数据清除 */
															et.setText("");
															button01.setEnabled(false);
															overlay.clear(true);
															/* 保存成功,显示Toast信息 */
															toast("保存成功,保存路径为:"
																	+ gesPath);
														} else {
															/* 保存失败,显示Toast信息 */
															toast("保存失败");
														}
													}
												}).show();
							} else {
								lib.addGesture(gesName, ges);
								if (lib.save()) {
									/* 将设定画面数据清除 */
									et.setText("");
									button01.setEnabled(false);
									overlay.clear(true);
									/* 保存成功,显示Toast信息 */
									toast("保存成功,保存路径为:" + gesPath);
								} else {
									/* 保存失败,显示Toast信息 */
									toast("保存失败");
								}
							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		/* 设置button02的OnClickListener */
		button02.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				et.setText("");
				button01.setEnabled(false);
				overlay.clear(true);
			}
		});
		button03.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				Intent intent = new Intent(MyGesture.this , ReadGesture.class);
				startActivity(intent);
			}
			
		});
	}

	public void toast(String str) {
		Toast.makeText(MyGesture.this, str, Toast.LENGTH_LONG).show();
	}

 ReadGesture.java

 

private SimpleAdapter listItemAdapter; // ListView的适配器
	private ArrayList<HashMap<String, Object>> listItem; // ListView的数据源,这里是一个HashMap的列表
	private ListView myList; // ListView控件
	
	private List<Gesture> gesList;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.phone_info);
		
		listItem = new ArrayList<HashMap<String, Object>>();
		listItemAdapter = new SimpleAdapter(this, listItem, R.layout.list_item3,
				new String[] { "image", "text" }, new int[] { R.id.imageView1,
						R.id.textView1 });
		myList = (ListView) findViewById(R.id.listView1);
		myList.setAdapter(listItemAdapter);
		
		gesList = new ArrayList<Gesture>();
		
		try{
			String gesPath = new File(Environment.getExternalStorageDirectory(),
				"gestures").getAbsolutePath();
			File gesFile = new File(gesPath);
			GestureLibrary lib = GestureLibraries.fromFile(gesFile);
			if(gesFile.exists()){
				if(!lib.load()){
					toast("读取Library失败");
				}
				else{
					Object[] entries = lib.getGestureEntries().toArray();
					for(int i = 0 ; i < entries.length ; i++){
						ArrayList<Gesture> gestureArray = lib.getGestures(entries[i].toString());
						for(int j = 0; j < gestureArray.size() ; j++){
							String gestureName = entries[i].toString();
							addItem(R.drawable.doc,gestureName);
							Gesture gesture = (Gesture)gestureArray.get(j);
							gesList.add(gesture);
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		myList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Bitmap bitmap = gesList.get(arg2).toBitmap(64, 64, 12, Color.BLUE);
				ImageView gesImage = new ImageView(ReadGesture.this);
				gesImage.setImageBitmap(bitmap);
				new AlertDialog.Builder(ReadGesture.this)
				.setTitle("查看Gesture")
				.setView(gesImage)
				.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialoginterface,
									int i) {
								dialoginterface.cancel();
							}
						}).show();
			}
			
		});
	}
	
	private void addItem(int image, String str) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("image", image);
		map.put("text", str);
		listItem.add(map);
		listItemAdapter.notifyDataSetChanged();
	}
	
	public void toast(String str) {
		Toast.makeText(ReadGesture.this, str, Toast.LENGTH_LONG).show();
	}




 

 

  • 大小: 23.3 KB
  • 大小: 25.4 KB
  • 大小: 19.1 KB
  • 大小: 22.6 KB
分享到:
评论

相关推荐

    GESTURE

    GESTURE

    Unity手写数字识别,录入 基于Gesture Recognize手势识别.zip

    1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也适合...

    unity 笔画识别

    PDollar Point-Cloud Gesture Recognizer unity中,写入mesh,记录过程点 坐标,同时给点坐标集合打标签,得到匹配项,检测的时候,当用户写了相应的字,程序识别出坐标点集合,跟之前记录的匹配项进行匹配,得出...

    Gesture手势添加与识别

    手势是通过特定的手指动作或触摸屏幕的方式表达的,这些动作可以是简单的滑动、点击,也可以是复杂的图案。Android系统提供了多种手势支持,包括基本的触摸事件(如ACTION_DOWN、ACTION_MOVE、ACTION_UP等)以及更...

    smooth gesture for chrome

    smooth gesture for chrome. chrome的插件

    创建一个名为RICK的手写符号

    通过使用GestureBuilder,开发者可以轻松地创建并保存各种手写笔画模式,以供日后使用。 #### GestureBuilder介绍 GestureBuilder是Google为Android开发者提供的一个用于创建和编辑手势的工具。它允许开发者绘制...

    GestureBuilder

    【GestureBuilder】是一款专为Android平台设计的手势创建工具,它允许用户自定义手势操作,并将其保存至SDCard(外部存储卡)的特定目录中,以便在应用中使用。通过这款工具,开发者或用户可以轻松地扩展应用程序的...

    smartgesture

    "SmartGesture"是一款专为Windows 7和Windows 8 64位系统设计的触摸板驱动程序,旨在提升用户在笔记本电脑上使用触摸板的体验。这个驱动程序包含了丰富的手势控制功能,使得用户可以通过简单的触摸板操作来实现复杂...

    UnityVR手势识别插件VR Infinite Gesture1.1.3

    UnityVR手势识别插件VR Infinite Gesture 1.1.3是一款专为虚拟现实(VR)环境设计的工具,主要用于在HTCVIVE平台上实现高级的手势交互。这款插件旨在提升用户体验,通过捕捉并解析用户的自然手势,使得用户能够在...

    unity手势识别系统源码Gesture Recognizer

    unity手势识别系统源码Gesture Recognizer Unity游戏源码 , Unity工具 , 完整的项目 , 适合学习和二次开发 。 是整个完整的UnityPackage包 , 使用新版本编译器请自行升级编译器设置就行 , C#语言的! C#语言的!! C#...

    SmartGesture

    【SmartGesture】是一款专为华硕笔记本电脑设计的触摸板驱动程序,旨在提供更为智能和灵敏的手势控制体验。这款驱动工具能够使用户通过简单的触摸板手势实现多种操作,极大地提升了在没有鼠标的情况下使用笔记本电脑...

    Android Gesture Builder

    Android手势构建器(Gesture Builder)是Android开发工具集的一部分,用于帮助开发者创建和管理自定义的手势识别。在Android应用中,手势识别可以提升用户体验,让用户通过简单的滑动、点击等动作来执行特定的操作。...

    gesturebuilder

    在Android平台上,手势Builder(GestureBuilder)是一种工具,它允许开发者创建和管理自定义的手势。这个工具可以用来构建手势库,这些库包含了各种手势,然后可以在应用程序中使用,以实现用户与应用的互动。通过...

    js touch触屏gesture手势demo

    JavaScript Touch事件和Gesture手势在移动设备开发中扮演着至关重要的角色,特别是在构建触摸友好型的Web应用时。本文将深入探讨“js touch触屏gesture手势demo”中的关键知识点,包括Touch事件模型、常见手势识别...

    Gesture recognition : principles, techniques and applications

    Gesture Driven Fuzzy Interface System for Car Racing Game....Pages 117-134 Type-2 Fuzzy Classifier Based Pathological Disorder Recognition....Pages 135-194 Probabilistic Neural Network Based Dance ...

    gesture-demo

    "gesture-demo"项目就是一个很好的实例,用于展示如何在Android应用中实现手势识别功能,非常适合初学者进行学习和实践。 首先,我们来了解一下Android手势的基本概念。手势是用户通过触摸屏幕并进行滑动、点击、...

    SmartGesture_WIN10_64_VER405

    【SmartGesture_WIN10_64_VER405】是一款专为ASUS笔记本电脑设计的触摸板驱动程序,主要用于优化Windows 10 64位系统的触摸板操作体验。此驱动程序的版本号为405,表明它可能包含了一些针对旧版驱动的改进和新功能的...

    SmartGesture_Win7_64_Z1032.zip

    标题“SmartGesture_Win7_64_Z1032.zip”揭示了这是一个适用于Windows 7 64位操作系统的华硕智能手势软件更新。"SmartGesture"是华硕为优化其笔记本电脑上的触摸板手势控制而开发的一个驱动程序和应用套件。此软件...

    VR Infinite Gesture——最新的版本

    【VR Infinite Gesture——最新的版本】 在虚拟现实(Virtual Reality,简称VR)技术的快速发展中,交互方式的创新一直是核心关注点。"VR Infinite Gesture" 是一款面向VR环境的交互解决方案,旨在提供更自然、直观...

Global site tag (gtag.js) - Google Analytics