AndroidStudio:手势识别

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

二实现

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

②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手势识别详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • Android Studio使用教程(二):基本设置与运行

    上面一篇博客,介绍了Studio的优点与1.0 RC的安装与上手体验,没想到google的更新速度这么快,已经出了RC 2版本,主要是修复一些bug.那么今天就带大家预览下Stduio的界面与基本功能. 项目结构 当我们新建一个项目的目录结构默认是这样的 可以看到和Eclipse的目录结构有很大区别,Studio一个窗口只能有一个项目,而Eclipse则可以同时存在很多项目,如果你看着不习惯可以点击左上角进行切换 切换到"project"模式下的目录结构是这样的,我个人也更习惯这种格式

  • Mac OS下为Android Studio编译FFmpeg解码库的详细教程

    NDK部分 1.下载ndk 这里就一笔带过了. 2.解压ndk 不要解压,文件权限会出错.执行之,会自动解压,然后mv到想放的地方.我放到了"/usr/local/bin/android-ndk-r10d"(此目录之后用$NDK_DIR指代). 3.下载Ffmpeg 我下的是2.5.3版本. 4.解压Ffmpeg 解压Ffmpeg到$NDK_DIR/sources/ffmpeg-2.5.3. 5.修改Ffmpeg编译配置 在ffmpeg-2.5.3目录下把configure文件中的这几

  • Android Studio使用小技巧:提取方法代码片段

    今天来给大家介绍一个非常有用的Studio Tips,有些时候我们在一个方法内部写了过多的代码,然后想要把一些代码提取出来再放在一个单独的方法里,通常我们的做法是复制粘贴,现在我来教给大家一个非常简洁的方法,先看下gif演示吧: 怎么样很方便吧?操作方法很简单,只需要选中一些代码,然后操作快捷键 Cmd + Option + M 就ok了,Windows是 Ctrl + Alt + M,很简单方便的一个小技巧分享给大家.

  • Android Studio使用小技巧:布局预览时填充数据

    我们都知道Android Studio用起来很棒,其中布局预览更棒.我们在调UI的时候基本是需要实时预览来看效果的,在Android Studio中只需要切换到Design就可以看到,而且我们需要在布局上填充数据预览效果更好,比如我们在TextView中设定text属性来看下字体大小与布局是否正确,但是呢正式环境我们又需要移除这些额外的数据,不然看着很不舒服,这个时候就用到了本篇博客介绍的一个技巧. 废话不多说,直接上图: 上述示例中只需要在xml布局文件中添加tools命名空间的text属性就

  • Android Studio使用教程(一):下载与安装及创建HelloWorld项目

    背景 相信大家对Android Studio已经不陌生了,Android Studio是Google于2013 I/O大会针对Android开发推出的新的开发工具,目前很多开源项目都已经在采用,Google的更新速度也很快,明显能感觉到这是Android开发的未来,那么我们还有什么理由不去拥抱未来呢? 虽然推出了很久,但是国内貌似普及的程度并不高,鉴于很多朋友求studio的详细教程,那么今天我就手把手教大家下载.安装.使用,Studio之路从这里开始. Android Studio VS Ec

  • Android Studio使用小技巧:自定义Logcat

    我们都知道Logcat是我们Android开发调试最常用的一个工具,但是Android Studio默认的Logcat调试的颜色是一样的,我们不好区分verbose.debug.error等分类信息,今天就来教大家自定义Logcat的提示信息. 打开Preference->Editor->Colors & Fonts->Android Logcat(或者搜索logcat),如果我们默认选择的是Darcula主题会看到如下界面: 这个默认的是无法更改,我们可以点击"Sav

  • 图解Windows环境下Android Studio安装和使用教程

    鉴于谷歌最新推出的Android Studio备受开发者的推崇,所以也跟着体验一下. 一.介绍Android Studio  Android Studio 是一个Android开发环境,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的 Android 开发工具用于开发和调试. 最近,Google 已宣布,为了简化 Android 的开发力度,以重点建设 Android Studio 工具,到今年年底将停止支持Eclipse等其他集成开发环

  • Android Studio的中文乱码问题解决方法

    Android Studio安装后发现所有的中文,不管是界面上的还是输出的log中的中文都变成小框框  可以肯定是字体的问题 解决:菜单File->settings对话框,切换到Appearance标签  选择override default fonts by, 先随便选择一个中文字体(PS:小框框都是中文字 呵呵),保存,重启软件,即可.

  • Android Studio注释模板介绍

    大家啊从Eclipse转到Android Studio很不习惯吧,感觉还是用Eclipse的方法注释模板比较方便,敲/**加回车,模板就加载出来了,而Android Studio却不能自定义,现在用live templates替代,具体方法通过图片和文字的方式展示如下: 步骤 1.File->Setting->Editor->Live Templates 2.点击+,创建一个Template Group 3.填个你要的group名,我的叫custom 4.选中你刚刚创建的group,创建

  • AndroidStudio 使用过程中出现的异常(Gradle sync failed)处理办法

    AndroidStudio使用过程中出现的异常 异常信息: Gradle sync failed: Unable to start the daemon process. This problem might be caused by incorrect configuration of the daemon. For example, an unrecognized jvm option is used. Please refer to the user guide chapter on th

随机推荐