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




 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值