建立自己的手写笔画图案

来源:互联网 发布:ubuntu terminal 字体 编辑:程序博客网 时间:2024/04/28 09:40

/*
 * 建立自己的手写笔画图案
 * Gesture对象是自GestureOverlay.getGesture()所取得的手写
 * 对象。GestureLibraries保存手写背后所包含的意义,程序中利
 * 用GestureLibraries.fromFile()方法来加载预设的Gesture文件
 * 倘若默认手机的SD存储卡中尚未创建Gesture手写数据文件,此程序
 * 也会处理创建新文件的工作。此外还应用了GestureLibraries.addGesture()
 * 新建手写数据;GestureLibraries.save()保存写入的手写数据
 * GestureLibraries.load()加载手写数据;GestureLibraries.removeGesture()
 * 删除手写数据等方法。
 */
import 略;

public class Ex05_25Activity extends Activity {private Gesture ges;private GestureLibrary lib;private GestureOverlayView overlay;private Button bt1, bt2;private EditText et;private String getPath;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 查看SD卡是否存在if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {// SD卡不存在,提示信息Toast.makeText(Ex05_25Activity.this, "SD卡不存在,程序无法执行!",Toast.LENGTH_SHORT).show();this.finish();}et = (EditText) findViewById(R.id.myEditText1);bt1 = (Button) findViewById(R.id.myButton1);bt2 = (Button) findViewById(R.id.myButton1);overlay = (GestureOverlayView) findViewById(R.id.myGestures1);// 取得系统默认的GestureLibrary文件路径getPath = new File(Environment.getExternalStorageDirectory(),"gestures").getAbsolutePath();// 设置EditText的onKeyListeneret.setOnKeyListener(new EditText.OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {// TODO Auto-generated method stub// 名称和手写都设置好时 将新建的Button enableif (ges != null && et.getText().length() != 0) {bt1.setEnabled(true);} else {bt1.setEnabled(false);}return false;}});// 设置overlay的OnGestureListeneroverlay.addOnGestureListener(new GestureOverlayView.OnGestureListener() {// 开始手写时将添加的Button disable,并清除Gesture@Overridepublic void onGestureStarted(GestureOverlayView overlay,MotionEvent event) {// TODO Auto-generated method stubbt1.setEnabled(false);ges = null;}// 手写完时判断名称和手写时候完整建立@Overridepublic void onGestureEnded(GestureOverlayView overlay,MotionEvent event) {// TODO Auto-generated method stubges = overlay.getGesture();if (ges != null && et.getText().length() != 0) {bt1.setEnabled(true);}}@Overridepublic void onGestureCancelled(GestureOverlayView overlay,MotionEvent event) {// TODO Auto-generated method stub}@Overridepublic void onGesture(GestureOverlayView overlay, MotionEvent event) {// TODO Auto-generated method stub}});bt1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString gesName = et.getText().toString();try {File file = new File(getPath);lib = GestureLibraries.fromFile(getPath);if (!file.exists()) {// 文件不存在就直接写入lib.addGesture(gesName, ges);if (lib.save()) {// 保存成功// 将设置画面数据清除et.setTag("");bt1.setEnabled(false);overlay.clear(true);Toast.makeText(Ex05_25Activity.this,getString(R.string.save_success) + ":"+ getPath, Toast.LENGTH_SHORT).show();} else {// 保存失败Toast.makeText(Ex05_25Activity.this,getString(R.string.save_failed) + ":"+ getPath, Toast.LENGTH_LONG).show();}} else {// 文件存在时,想读取以保存的Gestureif (!lib.load()) {// 读取失败Toast.makeText(Ex05_25Activity.this,getString(R.string.load_failed) + ":"+ getPath, Toast.LENGTH_SHORT).show();} else {// 读取成功// 如果存在相同的名称,则先将其移除再写入Set<String> en = lib.getGestureEntries();if (en.contains(gesName)) {ArrayList<Gesture> al = lib.getGestures(gesName);for (int i = 0; i < al.size(); i++) {lib.removeGesture(gesName, al.get(i));}}lib.addGesture(gesName, ges);if (lib.save()) {// 保存成功// 将设置画面数据清除et.setTag("");bt1.setEnabled(false);overlay.clear(true);Toast.makeText(Ex05_25Activity.this,getString(R.string.save_success) + ":"+ getPath, Toast.LENGTH_SHORT).show();} else {// 保存失败Toast.makeText(Ex05_25Activity.this,getString(R.string.save_failed) + ":"+ getPath, Toast.LENGTH_LONG).show();}}}} catch (Exception e) {e.printStackTrace();}}});bt2.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubet.setText("");bt1.setEnabled(false);overlay.clear(true);}});}}


main.xml文件代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/white"    android:orientation="vertical" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:id="@+id/myTextView1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/title"            android:textColor="@drawable/blue" />        <EditText            android:id="@+id/myEditText1"            android:layout_width="fill_parent"            android:layout_height="wrap_content" />    </LinearLayout>    <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        style="@android:style/ButtonBar"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/myButton1"            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="1"            android:enabled="false"            android:text="@string/button_done" />        <Button            android:id="@+id/myButton2"            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="@string/button_cancel" />    </LinearLayout></LinearLayout>


下面我们来看看程序运行后的结果:

原创粉丝点击