手势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

相关文章

解决docker目录内存不足扩容处理方案

《解决docker目录内存不足扩容处理方案》文章介绍了Docker存储目录迁移方法:因系统盘空间不足,需将Docker数据迁移到更大磁盘(如/home/docker),通过修改daemon.json配... 目录1、查看服务器所有磁盘的使用情况2、查看docker镜像和容器存储目录的空间大小3、停止dock

5 种使用Python自动化处理PDF的实用方法介绍

《5种使用Python自动化处理PDF的实用方法介绍》自动化处理PDF文件已成为减少重复工作、提升工作效率的重要手段,本文将介绍五种实用方法,从内置工具到专业库,帮助你在Python中实现PDF任务... 目录使用内置库(os、subprocess)调用外部工具使用 PyPDF2 进行基本 PDF 操作使用

分析 Java Stream 的 peek使用实践与副作用处理方案

《分析JavaStream的peek使用实践与副作用处理方案》StreamAPI的peek操作是中间操作,用于观察元素但不终止流,其副作用风险包括线程安全、顺序混乱及性能问题,合理使用场景有限... 目录一、peek 操作的本质:有状态的中间操作二、副作用的定义与风险场景1. 并行流下的线程安全问题2. 顺

Python绘制TSP、VRP问题求解结果图全过程

《Python绘制TSP、VRP问题求解结果图全过程》本文介绍用Python绘制TSP和VRP问题的静态与动态结果图,静态图展示路径,动态图通过matplotlib.animation模块实现动画效果... 目录一、静态图二、动态图总结【代码】python绘制TSP、VRP问题求解结果图(包含静态图与动态图

Python异常处理之避免try-except滥用的3个核心原则

《Python异常处理之避免try-except滥用的3个核心原则》在Python开发中,异常处理是保证程序健壮性的关键机制,本文结合真实案例与Python核心机制,提炼出避免异常滥用的三大原则,有需... 目录一、精准打击:只捕获可预见的异常类型1.1 通用异常捕获的陷阱1.2 精准捕获的实践方案1.3

Pandas处理缺失数据的方式汇总

《Pandas处理缺失数据的方式汇总》许多教程中的数据与现实世界中的数据有很大不同,现实世界中的数据很少是干净且同质的,本文我们将讨论处理缺失数据的一些常规注意事项,了解Pandas如何表示缺失数据,... 目录缺失数据约定的权衡Pandas 中的缺失数据None 作为哨兵值NaN:缺失的数值数据Panda

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别

Python动态处理文件编码的完整指南

《Python动态处理文件编码的完整指南》在Python文件处理的高级应用中,我们经常会遇到需要动态处理文件编码的场景,本文将深入探讨Python中动态处理文件编码的技术,有需要的小伙伴可以了解下... 目录引言一、理解python的文件编码体系1.1 Python的IO层次结构1.2 编码问题的常见场景二

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返

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

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