当前位置: 移动技术网 > 移动技术>移动开发>Android > AndroidStudio:手势识别

AndroidStudio:手势识别

2019年07月23日  | 移动技术网移动技术  | 我要评论
一内容:设计一个手写字体识别程序。 二实现 ①建立一个存放手写字体的数据库 ②activity_main.xml <?xml version=

一内容:设计一个手写字体识别程序。

二实现

①建立一个存放手写字体的数据库

②activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".mainactivity"
  android:orientation="vertical">
 
  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="gesture:"
    android:id="@+id/tv"
    android:textsize="24dp"/>
 
  <button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textsize="20dp"
    android:text="clear"
    android:id="@+id/bt"/>
 
  <android.gesture.gestureoverlayview
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gesturestroketype="multiple"
    android:eventsinterceptionenabled="false"
    android:orientation="vertical"
    android:id="@+id/gesture"></android.gesture.gestureoverlayview>
</linearlayout

3.mainactivity.java

package com.example.myapplication;
 
import android.gesture.gesture;
import android.gesture.gesturelibraries;
import android.gesture.gesturelibrary;
import android.gesture.gestureoverlayview;
import android.gesture.prediction;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.textview;
import android.widget.toast;
 
import java.util.arraylist;
 
public class mainactivity extends appcompatactivity implements gestureoverlayview.ongestureperformedlistener {
  gesturelibrary mlibrary; //定义手势库对象
  gestureoverlayview gest; //定义手势视图对象做画板之用
  textview txt;
  button bt;
 
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
 
    gest = (gestureoverlayview)findviewbyid(r.id.gesture);
    gest.addongestureperformedlistener(this); // 注册手势识别的监听器
    txt = (textview)findviewbyid(r.id.tv);
    mlibrary = gesturelibraries.fromrawresource(this,r.raw.gestures); //加载手势库
    bt = (button)findviewbyid(r.id.bt);
    bt.setonclicklistener(new click());
 
    if (!mlibrary.load()) {
      finish();
    }
  }
    /*根据画的手势识别是否匹配手势库里的手势*/
  @override
  public void ongestureperformed(gestureoverlayview gest, gesture gesture) {
    arraylist gestlist = mlibrary.recognize(gesture); // 从手势库获取手势数据
    if (gestlist.size() > 0) {
      prediction pred = (prediction)gestlist.get(0);
      if (pred.score > 1.0) {  // 检索到匹配的手势
        toast.maketext(this,pred.name,toast.length_short).show();
        txt.append(pred.name);
      }
    }
  }
 
  private class click implements view.onclicklistener {
    @override
    public void onclick(view view) {
      txt.settext("gesture:");
    }
  }
}

三效果

以上所述是小编给大家介绍的androidstudio手势识别详解整合,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网