AndroidStudio:手势识别

所属分类: 软件编程 / Android 阅读数: 91
收藏 0 赞 0 分享

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

二实现

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

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

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多