手势Gesture的绘制、保存及处理的学习

2024-04-14 19:58

本文主要是介绍手势Gesture的绘制、保存及处理的学习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近写了一个小程序,学习了一下android中的手势文件。手势相关的类有
1.GestureOverlayView,这个是手势绘制区,既布局中的一个控件,用于接收用户绘制的手势、监听绘制区的改变、清除当前手势等等。
2.GestureLibrary 这个算是手势文件的一个库,里面存放着当前保存过的手势资源,可以用这个类进行手势资源的存储和读取。
3.Gesture,手势实例,无论是读取手势,还是要保存当前的手势,都是Gesture对象。
4.Prediction 识别率。主要用于在根据当前手势查询手势库中是否有匹配手势时需要用到。
下面根据程序来详细讲解一下如何应用这几个类。

本主要功能是根据手势判别来拨打电话。可以保存手势,查看现在保存的手势,如果程序第一次运行,便在指定路径下建立手势文件。



因为要保存手势文件和打电话,所以首先在程序清单中添加权限
AndroidManifest.xml代码片段:
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  2.         <uses-permission android:name="android.permission.CALL_PHONE"/>
复制代码
程序用到的布局文件有4个,一个是主Activity用到的xml布局。就是第一张图的布局文件:
main.xml代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical" >

  6.     <TextView
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:text="@string/hello" />

  10.     <android.gesture.GestureOverlayView
  11.         android:id="@+id/gestures"
  12.         android:layout_width="fill_parent"
  13.         android:layout_height="fill_parent"
  14.         android:layout_weight="1"
  15.         android:gestureStrokeType="multiple" >
  16.     </android.gesture.GestureOverlayView>

  17. <LinearLayout  android:layout_width="fill_parent"
  18.             android:layout_height="wrap_content"
  19.             android:gravity="center_horizontal">
  20.         <Button
  21.             android:id="@+id/btnAdd"
  22.             android:layout_width="wrap_content"
  23.             android:layout_height="wrap_content"
  24.             android:text="    添加手势    " />
  25.           <Button
  26.             android:id="@+id/btnSelect"
  27.             android:layout_width="wrap_content"
  28.             android:layout_height="wrap_content"
  29.             android:text="查看已有手势" />
  30. </LinearLayout>

  31. </LinearLayout>
复制代码
以及添加手势资源时用的布局文件:
addgesture.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical"
  6.     android:gravity="center_horizontal">

  7.     <LinearLayout
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content" >

  10.         <TextView
  11.             android:layout_width="wrap_content"
  12.             android:layout_height="wrap_content"
  13.             android:text="手机号码:"
  14.             android:inputType="number"/>

  15.         <EditText
  16.             android:id="@+id/txtNum"
  17.             android:layout_width="match_parent"
  18.             android:layout_height="wrap_content" />
  19.     </LinearLayout>

  20.     <android.gesture.GestureOverlayView
  21.         android:id="@+id/gestureAdd"
  22.         android:layout_width="fill_parent"
  23.         android:layout_height="fill_parent"
  24.         android:layout_weight="1"
  25.         android:gestureStrokeType="multiple" >
  26.     </android.gesture.GestureOverlayView>

  27.     <Button
  28.         android:id="@+id/btnOK"
  29.         android:layout_width="wrap_content"
  30.         android:layout_height="wrap_content"
  31.         android:text="    确定    " />

  32. </LinearLayout>
复制代码
剩下2个是gridView的布局文件和点击查看所有手势的布局文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical"
  6.     android:gravity="center_horizontal">

  7.     <LinearLayout
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content" >

  10.         <TextView
  11.             android:layout_width="wrap_content"
  12.             android:layout_height="wrap_content"
  13.             android:text="手机号码:"
  14.             android:inputType="number"/>

  15.         <EditText
  16.             android:id="@+id/txtNum"
  17.             android:layout_width="match_parent"
  18.             android:layout_height="wrap_content" />
  19.     </LinearLayout>

  20.     <android.gesture.GestureOverlayView
  21.         android:id="@+id/gestureAdd"
  22.         android:layout_width="fill_parent"
  23.         android:layout_height="fill_parent"
  24.         android:layout_weight="1"
  25.         android:gestureStrokeType="multiple" >
  26.     </android.gesture.GestureOverlayView>

  27.     <Button
  28.         android:id="@+id/btnOK"
  29.         android:layout_width="wrap_content"
  30.         android:layout_height="wrap_content"
  31.         android:text="    确定    " />

  32. </LinearLayout>
复制代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >

  6.     <GridView
  7.         android:id="@+id/gvTop"
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="fill_parent"
  10.         android:gravity="center"
  11.         android:numColumns="auto_fit"
  12.         android:scrollbars="none">
  13.     </GridView>

  14. </LinearLayout>
复制代码
在主activity GestureLearnActivity中,要判断第一次加载时是否有手势文件,如果没有则创建一个手势文件,并监听手势绘制区的改变,当绘制完成时与手势库中的手势进行比较。
GestureLearnActivity代码片段
  1. /**
  2.          * 初始化
  3.          */
  4.         public void init() {
  5.                 // 获得布局中的组件
  6.                 btnAdd = (Button) findViewById(R.id.btnAdd);
  7.                 btnAdd.setOnClickListener(this);
  8.                 btnSelect = (Button) findViewById(R.id.btnSelect);
  9.                 btnSelect.setOnClickListener(this);
  10.                 gesture = (GestureOverlayView) findViewById(R.id.gestures);
  11.                 // 手势文件的加载路径
  12.                 String path = "/sdcard/gestures";
  13.                 // 加载手势文件
  14.                 library = GestureLibraries.fromFile(path);
  15.                 if (library.load()) {
  16.                        
  17.                         gesture.addOnGesturePerformedListener(this);
  18.                 } else {
  19.                         Toast.makeText(GestureLearnActivity.this, "无手势文件",
  20.                                         Toast.LENGTH_LONG).show();
  21.                        
  22.                         File file = new File(path);
  23.                         try {
  24.                                 //创建手势文件
  25.                                 file.createNewFile();
  26.                         } catch (Exception e) {
  27.                                 e.printStackTrace();
  28.                         }
  29.                 }
  30.         }
  31. // 这个接口里处理匹配的手势
  32. public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
  33.         ArrayList<Prediction> predictions = library.recognize(gesture);
  34.         if (predictions.size() > 0) {
  35.                 // 获得识别率
  36.                 Prediction predict = predictions.get(0);
  37.                 // 如果识别率大于1,则说明找到匹配手势
  38.                 if (predict.score > 1.0) {
  39.                         //调用打电话activity
  40.                         Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
  41.                                         + predict.name));
  42.                         //不打电话,只进入打电话界面
  43.                         //Intent phone=new Intent("com.android.phone.action.TOUCH_DIALER");
  44.                         startActivity(intent);
  45.                         }
  46.                 }

  47.         }
复制代码
在点击添加手势按钮以后,程序进入第二个activity中
GestureLearnActivity代码片段:
  1. case R.id.btnAdd:// 处理按下添加手势按钮
  2.                         Intent intent = new Intent(getApplicationContext(),
  3.                                         AddGesture.class);
  4.                         startActivity(intent);
  5.                         //切换activity动画
  6.                         overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_in_left);
  7.                        
  8.                         break;
复制代码
添加手势的activity中实现添加手势绘制区的监听,需要实现4个方法
AddGesture代码片段:
  1. /**
  2.          * 处理监听,初始化组件
  3.          */
  4.         public void init() {
  5.                 gestureNew = (GestureOverlayView) findViewById(R.id.gestureAdd);
  6.                 txtNum = (EditText) findViewById(R.id.txtNum);
  7.                 btnOk = (Button) findViewById(R.id.btnOK);
  8.                 btnOk.setOnClickListener(new View.OnClickListener() {

  9.                         public void onClick(View v) {
  10.                                 // 返回按钮。
  11.                                 finish();
  12.                         }
  13.                 });
  14.                 // 添加手势绘制区的监听,需要实现4个方法
  15.                 gestureNew
  16.                                 .addOnGestureListener(new GestureOverlayView.OnGestureListener() {
  17.                                         // 开始绘制时调用
  18.                                         public void onGestureStarted(GestureOverlayView overlay,
  19.                                                         MotionEvent event) {

  20.                                         }

  21.                                         // 以下方法是当手势完整形成的时候触发,主要处理在这个方法中实现
  22.                                         public void onGestureEnded(GestureOverlayView overlay,
  23.                                                         MotionEvent event) {
  24.                                                 Log.e("sc", "进入绘制完成");
  25.                                                 gesture = overlay.getGesture();
  26.                                                 //如果没有输入
  27.                                                 if (txtNum.getText().toString().equals("")) {
  28.                                                         Toast.makeText(AddGesture.this, "请输入号码",
  29.                                                                         Toast.LENGTH_LONG).show();
  30.                                                         txtNum.setFocusable(true);
  31.                                                         gestureNew.clear(true);

  32.                                                 }
  33.                                                 //利用正则表达试判断输入
  34.                                                 else if (!(txtNum.getText().toString()
  35.                                                                 .matches("[0-9]*"))) {
  36.                                                         Toast.makeText(AddGesture.this, "输入的必须是数字",
  37.                                                                         Toast.LENGTH_LONG).show();
  38.                                                         txtNum.setFocusable(true);

  39.                                                 } else {
  40.                                                         // 读取文件
  41.                                                         libraryNew = GestureLibraries
  42.                                                                         .fromFile("/sdcard/gestures");
  43.                                                         Log.e("sc", "读取文件完毕" + libraryNew.toString());
  44.                                                         libraryNew.load();
  45.                                                         // 给手势仓库添加当前的手势
  46.                                                         libraryNew.addGesture(txtNum.getText().toString(),
  47.                                                                         gesture);
  48.                                                         if (libraryNew.save())// 保存
  49.                                                         {
  50.                                                                 gestureNew.clear(true);// 清除笔画
  51.                                                                 Toast.makeText(AddGesture.this, "保存成功!",
  52.                                                                                 Toast.LENGTH_LONG).show();
  53.                                                         } else {
  54.                                                                 Toast.makeText(AddGesture.this, "保存失败",
  55.                                                                                 Toast.LENGTH_LONG).show();
  56.                                                         }
  57.                                                 }
  58.                                         }

  59.                                         public void onGestureCancelled(GestureOverlayView overlay,
  60.                                                         MotionEvent event) {
  61.                                                 // TODO Auto-generated method stub

  62.                                         }

  63.                                         public void onGesture(GestureOverlayView overlay,
  64.                                                         MotionEvent event) {
  65.                                                 // TODO Auto-generated method stub

  66.                                         }
  67.                                 });
  68.         }
复制代码
剩下的就是查看手势文件了,这里我用到的是GridView控件来布局,将手势库中的文件都读取出来,由于是练习程序,所以没做复杂的操作,只是显示已经保存的手势文件。这里用到Gesture 的实例对象,通过这个对象将手势文件转换成位图来显示。
ListGestures.java代码片段
  1. public void load() {
  2.                 String path = "/sdcard/gestures";
  3.                 // 加载手势文件
  4.                 library = GestureLibraries.fromFile(path);

  5.                 if (library.load()) {
  6.                         int index = library.getGestureEntries().size();
  7.                         pics = new Bitmap[index];
  8.                         gesName=new String[index];
  9.                         int i = 0;
  10.                         //获得所有手势文件,返回的是存储key的set集合
  11.                         for (String name : library.getGestureEntries()) {
  12.                                 // 因为在手势仓库中,支持一个name对应多个手势文件,
  13.                                 // 所以会返回一个list,在这里我们取list里的第一个
  14.                                 ArrayList<Gesture> geess = library.getGestures(name);
  15.                                 Gesture gg = geess.get(0);
  16.                                 //将手势文件转成位图
  17.                                 Bitmap bmp = gg.toBitmap(100, 100, 12, Color.BLUE);
  18.                                 pics[i] = bmp;//保存位图
  19.                                 gesName[i]=name;//保存当前的手势名称。
  20.                                 i++;
  21.                         }
  22.                 }
  23.         }
复制代码
最后的gridView的适配器代码就不多说了。直接上源码
  1. public class GridAdapter extends BaseAdapter {
  2.         // 每个gridItem显示的值
  3.         private Context mContext;
  4.         private LayoutInflater mInflater;

  5.         public GridAdapter(Context context) {
  6.                 mContext = context;
  7.                 mInflater = (LayoutInflater) context
  8.                                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  9.         }

  10.         public int getCount() {
  11.                 // 返回适配器中数据的数量
  12.                 return ListGestures.pics.length;
  13.         }

  14.         public Object getItem(int position) {
  15.                 // 用不到
  16.                 return null;
  17.         }

  18.         public long getItemId(int position) {
  19.                 // 用不到
  20.                 return 0;
  21.         }

  22.         // 此方法的convertView是在grid_item里定义的组件。这里是一个ImageView和TextView
  23.         public View getView(int position, View convertView, ViewGroup parent) {
  24.                 if (convertView == null) {
  25.                         convertView = mInflater.inflate(R.layout.grid_item, null);
  26.                 }
  27.                 ImageView icon = (ImageView) convertView.findViewById(R.id.itemIcon);
  28.                 TextView text = (TextView) convertView.findViewById(R.id.itemText);
  29.                 icon.setImageBitmap(ListGestures.pics[position]);
  30.                 text.setText(ListGestures.gesName[position]);
  31.                 return convertView;// 返回已经改变过的convertView,节省系统资源
  32.         }

  33. }
复制代码

这篇关于手势Gesture的绘制、保存及处理的学习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/903871

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

Python自动化处理PDF文档的操作完整指南

《Python自动化处理PDF文档的操作完整指南》在办公自动化中,PDF文档处理是一项常见需求,本文将介绍如何使用Python实现PDF文档的自动化处理,感兴趣的小伙伴可以跟随小编一起学习一下... 目录使用pymupdf读写PDF文件基本概念安装pymupdf提取文本内容提取图像添加水印使用pdfplum

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

基于Redis自动过期的流处理暂停机制

《基于Redis自动过期的流处理暂停机制》基于Redis自动过期的流处理暂停机制是一种高效、可靠且易于实现的解决方案,防止延时过大的数据影响实时处理自动恢复处理,以避免积压的数据影响实时性,下面就来详... 目录核心思路代码实现1. 初始化Redis连接和键前缀2. 接收数据时检查暂停状态3. 检测到延时过

Java利用@SneakyThrows注解提升异常处理效率详解

《Java利用@SneakyThrows注解提升异常处理效率详解》这篇文章将深度剖析@SneakyThrows的原理,用法,适用场景以及隐藏的陷阱,看看它如何让Java异常处理效率飙升50%,感兴趣的... 目录前言一、检查型异常的“诅咒”:为什么Java开发者讨厌它1.1 检查型异常的痛点1.2 为什么说