android创建手势识别示例代码

时间:2022-01-08 07:30:54

这篇的内容使用到的是android.gesture包,具体的例子参考的是sample中gesturebuilder程序。

1、手势创建
手势创建主要用到gestureoverlayview和gesturelibrary。gestureoverlayview的父类为android.widget.framelayout,是手势绘图区。gesturelibrary类主要对手势进行保存、删除等操作的,存放手势的仓库。下面给出创建手势的例子,如下图,可以定义如图手势打开csdn.net

android创建手势识别示例代码

1.1、创建绘图区

 

复制代码 代码如下:


 gestureoverlayview overlay = (gestureoverlayview) findviewbyid(r.id.gestures_overlay);

overlay.setgesturestroketype(gestureoverlayview.gesture_stroke_type_multiple); 

overlay.setfadeoffset(2000);  //多笔画每两次的间隔时间
overlay.setgesturecolor(color.cyan);//画笔颜色
overlay.setgesturestrokewidth(6);//画笔粗细值

 

overlay.addongesturelistener(new gesturesprocessor());

 

1.2、监听绘制

 

复制代码 代码如下:


 private class gesturesprocessor implements gestureoverlayview.ongesturelistener {
        public void ongesturestarted(gestureoverlayview overlay, motionevent event) {
            mdonebutton.setenabled(false);
            mgesture = null;
        }

 

        public void ongesture(gestureoverlayview overlay, motionevent event) {
        }

        public void ongestureended(gestureoverlayview overlay, motionevent event) {
            mgesture = overlay.getgesture();
            if (mgesture.getlength() < length_threshold) {
                overlay.clear(false);
            }
            mdonebutton.setenabled(true);
        }

        public void ongesturecancelled(gestureoverlayview overlay, motionevent event) {
        }
    }

 

1.3、保存手势

 

复制代码 代码如下:


 public void addgesture(view v) {
        if (mgesture != null) {
            final textview input = (textview) findviewbyid(r.id.gesture_name);
            final charsequence name = input.gettext();
            if (name.length() == 0) {
                input.seterror(getstring(r.string.error_missing_name));
                return;
            }

 

            /**
             * 获取手势库
             *   private final file mstorefile = new file(environment.getexternalstoragedirectory(), "gestures");
             *   gesturelibrary sstore = gesturelibraries.fromfile(mstorefile);
             *
             */
            final gesturelibrary store = gesturebuilderactivity.getstore();
            store.addgesture(name.tostring(), mgesture);
            store.save();

            setresult(result_ok);

            final string path = new file(environment.getexternalstoragedirectory(),
                    "gestures").getabsolutepath();
            toast.maketext(this, getstring(r.string.save_success, path), toast.length_long).show();
        } else {
            setresult(result_canceled);
        }

        finish();

    }

 

2、手势识别

手势识别也是经过创建绘图区、监听绘制、比对结果三个过程,这里直接给出代码。

 

复制代码 代码如下:


public class gestureperformedactivity extends activity {

 

 private final file mstorefile = new file(
   environment.getexternalstoragedirectory(), "gestures");

 // 手势库
 gesturelibrary mgesturelib;

 @override
 protected void oncreate(bundle savedinstancestate) {
  // todo auto-generated method stub
  super.oncreate(savedinstancestate);

  setcontentview(r.layout.gesture_perform);
  // 手势画板
  gestureoverlayview gestures = (gestureoverlayview) findviewbyid(r.id.gestures_overlay);

  gestures.setgesturestroketype(gestureoverlayview.gesture_stroke_type_multiple);

  gestures.setfadeoffset(2000); // 多笔画每两次的间隔时间
  gestures.setgesturecolor(color.cyan);// 画笔颜色
  gestures.setgesturestrokewidth(6);// 画笔粗细值

  // 手势识别的监听器
  gestures.addongestureperformedlistener(new gestureoverlayview.ongestureperformedlistener() {
   @override
   public void ongestureperformed(gestureoverlayview overlay,
     gesture gesture) {
    // 从手势库中查询匹配的内容,匹配的结果可能包括多个相似的结果,匹配度高的结果放在最前面
    arraylist<prediction> predictions = mgesturelib
      .recognize(gesture);  
    if (predictions.size() > 0) {
     prediction prediction = (prediction) predictions.get(0);
     // 匹配的手势
     if (prediction.score > 1.0) { // 越匹配score的值越大,最大为10
      toast.maketext(gestureperformedactivity.this,
        prediction.name, toast.length_short).show();
     }
    }
   }
  });

  if (mgesturelib == null) {
   mgesturelib = gesturelibraries.fromfile(mstorefile);
   mgesturelib.load();
  }
 }

}



android创建手势识别示例代码