经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android实现自定义手势和识别手势的功能
来源:jb51  时间:2019/10/12 9:02:26  对本文有异议

1. 先完成自定义手势的Activity

1.1 因为需要存储手势文件所以需要声明权限:

  1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> //读取SD卡权限
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> //写入SD卡权限

1.2 简单写一个布局文件,其中用到了GestureOverlayView,相当于一个绘制组件。其中有一个重要属性gestureStrokeType,值为single时表示只绘制一笔,若要多笔绘制值应该设为multiple:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".addgesture.Main3Activity">
  9. <Button
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:onClick="recognition"
  13. android:text="识别手势" />
  14. <TextView
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:gravity="center"
  18. android:text="请绘制手势" />
  19. <android.gesture.GestureOverlayView
  20. android:id="@+id/activity_main3_gov"
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"
  23. android:gestureStrokeType="multiple" //多笔绘制
  24. ></android.gesture.GestureOverlayView>
  25. </LinearLayout>

1.3 这里自定义了AlertDialog的样式;

  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. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content">
  9. <TextView
  10. android:layout_width="wrap_content"
  11. android:layout_height="match_parent"
  12. android:gravity="center"
  13. android:text="请输入手势名称" />
  14. <EditText //输入手势的名称
  15. android:id="@+id/save_dialog_et"
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent" />
  18. </LinearLayout>
  19. <ImageView //展示绘制的手势
  20. android:id="@+id/save_dialog_iv"
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent" />
  23. </LinearLayout>

1.4 代码部分:

  1. package com.example.mygesture.addgesture;
  2. import android.Manifest;
  3. import android.content.DialogInterface;
  4. import android.content.Intent;
  5. import android.content.pm.PackageManager;
  6. import android.gesture.Gesture;
  7. import android.gesture.GestureLibraries;
  8. import android.gesture.GestureLibrary;
  9. import android.gesture.GestureOverlayView;
  10. import android.graphics.Bitmap;
  11. import android.graphics.Color;
  12. import android.support.v4.app.ActivityCompat;
  13. import android.support.v7.app.AlertDialog;
  14. import android.support.v7.app.AppCompatActivity;
  15. import android.os.Bundle;
  16. import android.view.View;
  17. import android.widget.EditText;
  18. import android.widget.ImageView;
  19. import android.widget.Toast;
  20. import com.example.mygesture.R;
  21. import com.example.mygesture.recognitiongesture.Main4Activity;
  22. public class Main3Activity extends AppCompatActivity {
  23. GestureOverlayView gov; //定义绘制组件
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main3);
  28. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
  29. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
  30. } //高版本需要动态申请权限
  31. init();
  32. }
  33. private void init() {
  34. gov = findViewById(R.id.activity_main3_gov);
  35. // gov.setGestureColor(Color.RED); //设置绘制的颜色
  36. gov.setGestureStrokeWidth(4); //设置画笔的宽度
  37. gov.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() { //设置绘制完成监听
  38. @Override
  39. public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {
  40. View saveDialog = getLayoutInflater().inflate(R.layout.save_dialog, null); //获取AlertDialog的布局样式
  41. final EditText editText = saveDialog.findViewById(R.id.save_dialog_et);
  42. ImageView imageView = saveDialog.findViewById(R.id.save_dialog_iv);
  43. Bitmap bitmap = gesture.toBitmap(128, 128, 10, 0xFFFF0000); //将手势转换为位图
  44. imageView.setImageBitmap(bitmap); //用ImageView加载手势图片
  45. new AlertDialog.Builder(Main3Activity.this).setView(saveDialog).setPositiveButton("确定", new DialogInterface.OnClickListener() {
  46. @Override
  47. public void onClick(DialogInterface dialog, int which) {
  48. GestureLibrary gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/mygesture");//利用手势库获取存放手势文件的地址
  49. gestureLibrary.addGesture(editText.getText().toString(), gesture); //向手势库中添加手势名称和手势
  50. gestureLibrary.save(); //保存手势库
  51. Toast.makeText(Main3Activity.this, "保存成功", Toast.LENGTH_SHORT).show();
  52. }
  53. }).setNegativeButton("取消", null)
  54. .show();
  55. }
  56. });
  57. }
  58. public void recognition(View view) {
  59. Intent intent = new Intent(this, Main4Activity.class);
  60. startActivity(intent);
  61. }
  62. }

2. 接下来完成识别手势的Activity:

2.1 一样的先写布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".recognitiongesture.Main4Activity">
  9.  
  10. <TextView
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:gravity="center"
  14. android:text="请绘制需要识别的手势" />
  15.  
  16. <android.gesture.GestureOverlayView
  17. android:id="@+id/activity_main4_gov"
  18. android:layout_width="match_parent"
  19. android:layout_height="match_parent"></android.gesture.GestureOverlayView>
  20. </LinearLayout>

2.2 代码的编写

  1. package com.example.mygesture.recognitiongesture;
  2. import android.gesture.Gesture;
  3. import android.gesture.GestureLibraries;
  4. import android.gesture.GestureLibrary;
  5. import android.gesture.GestureOverlayView;
  6. import android.gesture.Prediction;
  7. import android.support.v7.app.AlertDialog;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.Toast;
  12. import com.example.mygesture.R;
  13. import java.util.ArrayList;
  14. import java.util.logging.Level;
  15. public class Main4Activity extends AppCompatActivity {
  16. GestureOverlayView gov;
  17. GestureLibrary gestureLibrary; //定义手势库
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main4);
  22. init();
  23. }
  24. private void init() {
  25. gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/mygesture"); //获取手势文件
  26. if (gestureLibrary.load()) { //判断手势文件是否存在以及加载
  27. Toast.makeText(this, "手势文件加载成功", Toast.LENGTH_SHORT).show();
  28. } else {
  29. Toast.makeText(this, "手势文件加载失败", Toast.LENGTH_SHORT).show();
  30. }
  31. gov = findViewById(R.id.activity_main4_gov);
  32. gov.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
  33. @Override
  34. public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
  35. ArrayList<Prediction> predictions = gestureLibrary.recognize(gesture); //匹配手势库中的所有手势
  36. ArrayList<String> result = new ArrayList<>(); //匹配结果数组
  37. for (Prediction pred : predictions) {
  38. if (pred.score > 2) { //匹配手势库中的所有手势,并将相似度>2存入匹配结果数组
  39. result.add("相似度:" + pred.score);
  40. }
  41. }
  42. if (result.size() > 0) { //这里用了适配器来作为AlertDialog的布局样式,用于显示所有手势的相似度
  43. ArrayAdapter<Object> arrayAdapter = new ArrayAdapter<Object>(Main4Activity.this, android.R.layout.simple_dropdown_item_1line, result.toArray());
  44. new AlertDialog.Builder(Main4Activity.this).setAdapter(arrayAdapter, null).setPositiveButton("确定", null).show();
  45. } else {
  46. Toast.makeText(Main4Activity.this, "未找到与之匹配的手势", Toast.LENGTH_SHORT).show();
  47. }
  48. }
  49. });
  50. }
  51. }

总结

以上所述是小编给大家介绍的Android实现自定义手势和识别手势的功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对w3xue网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号