zoukankan      html  css  js  c++  java
  • 030、手写笔画的保存和读取(GestureOverlayView)

    Android手机自SDK1.6开始支持内置Gesture Builder程序,提供了手写识别功能,让用户以类似涂鸦的方式绘制一个手写符号,使之对应一个字符串名称。
    本例通过GestureOverlayView控件来接收用户的手写
    1、手写笔画的生成保存:
    效果图:
     
    布局文件中GestureOverlayView:
        <android.gesture.GestureOverlayView
            android:id="@+id/gov"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/ll_bottom"
            android:layout_below="@id/ll_top"
            android:gestureStrokeType="multiple" >
        </android.gesture.GestureOverlayView>
    默认是支持一笔,的添加android:gestureStrokeType="multiple"可以设置为支持多笔画
     
    java代码:
        private EditText et;
        private GestureOverlayView gov;
        private Gesture gesture;
        private GestureLibrary gestureLibrary;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // 判断是否有SD卡,无SD卡,则不运行该程序
            if (!Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                Toast.makeText(this, "无SD卡,不能保存画好的图片", 0).show();
                finish();
            }
            findViews();
            setListeners();
        }
        private void findViews() {
            et = (EditText) findViewById(R.id.et);
            gov = (GestureOverlayView) findViewById(R.id.gov);
        }
        private void setListeners() {
            findViewById(R.id.bt_add).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name = et.getText().toString().trim();
                    if (!TextUtils.isEmpty(name) && gesture != null) {
                        File file = new File(Environment
                                .getExternalStorageDirectory(), "gestures");
                        gestureLibrary = GestureLibraries.fromFile(file);
                        if (file.exists()) {
                            // 文件存在,应该先读取保存的gesture
                            if (gestureLibrary.load()) {
                                Set<String> gestureEntries = gestureLibrary
                                        .getGestureEntries();
                                if (gestureEntries.contains(name)) {
                                    ArrayList<Gesture> gestures = gestureLibrary
                                            .getGestures(name);
                                    for (Gesture ges : gestures) {
                                        gestureLibrary.removeGesture(name, ges);
                                    }
                                }
                                gestureLibrary.addGesture(name, gesture);
                                saveToast(name);
                            } else {
                                Toast.makeText(MainActivity.this, "读取失败", 0).show();
                            }
                        } else {
                            // 文件不存在可以直接存入
                            gestureLibrary.addGesture(name, gesture);
                            saveToast(name);
                        }
                    } else {
                        Toast.makeText(MainActivity.this, "文件名和图画都不能空哦", 0).show();
                    }
                }
            });
            findViewById(R.id.bt_reset).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    et.setText("");
                    gov.clear(true);
                }
            });
            
            findViewById(R.id.bt_show).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, GestureListActivity.class);
                    startActivity(intent);
                }
            });
            /**
             * 设置GestureOverlayView的OnGestureListener监听
             */
            gov.addOnGestureListener(new OnGestureListener() {
                @Override
                public void onGestureStarted(GestureOverlayView overlay,
                        MotionEvent event) {
                    gesture = null;
                }
                @Override
                public void onGestureEnded(GestureOverlayView overlay,
                        MotionEvent event) {
                    gesture = overlay.getGesture();
                }
                @Override
                public void onGestureCancelled(GestureOverlayView overlay,
                        MotionEvent event) {
                }
                @Override
                public void onGesture(GestureOverlayView overlay, MotionEvent event) {
                }
            });
        }
        
        private void saveToast(String name) {
            if (gestureLibrary.save()) {
                // 将设定画面数据清除
                et.setText("");
                gov.clear(true);
                Toast.makeText(MainActivity.this, name + "保存成功", 0)
                        .show();
            } else {
                Toast.makeText(MainActivity.this, name + "保存失败", 0)
                        .show();
            }
        }
     
    2、获取已保存的笔画显示:
    java代码如下:
        private void loadGestures() {
            titles.clear();
            images.clear();
            File file = new File(Environment.getExternalStorageDirectory(),
                    "gestures");
            GestureLibrary gestureLibrary = GestureLibraries.fromFile(file);
            if (file.exists()) {
                // 文件存在时,先读取
                if (gestureLibrary.load()) {
                    tv.setVisibility(View.GONE);
                    // 读取gesture信息
                    Object[] array = gestureLibrary.getGestureEntries().toArray();
                    for (Object obj : array) {
                        ArrayList<Gesture> gestures = gestureLibrary
                                .getGestures(obj.toString());
                        for (Gesture ges : gestures) {
                            titles.add(obj.toString());
                            images.add(ges.toBitmap(80, 80, 5, Color.GREEN));
                        }
                    }
                } else {
                    // 读取失败
                    tv.setVisibility(View.VISIBLE);
                    tv.setText("读取失败");
                }
            } else {
                // 读取失败
                tv.setVisibility(View.VISIBLE);
                tv.setText("还没有保存过吧");
            }
        }
  • 相关阅读:
    HTTP的连接过程
    查看mysql数据库及表编码格式
    spring quartz 的定时器cronExpression表达式写法(转载)
    两种 js下载文件的步骤
    进制转换三(二进制、八进制、十进制、十六进制之间的转换)
    进制的转换二
    Tls版本
    读李笑来《把时间当做朋友》笔记
    postman批量编辑参数
    uml类关系常见图标的含义
  • 原文地址:https://www.cnblogs.com/zyh-blog/p/3345584.html
Copyright © 2011-2022 走看看