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

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

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

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

<?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"
 android:orientation="vertical"
 tools:context=".addgesture.Main3Activity">
 <Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:onClick="recognition"
  android:text="识别手势" />
 <TextView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text="请绘制手势" />
 <android.gesture.GestureOverlayView
  android:id="@ id/activity_main3_gov"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gestureStrokeType="multiple" //多笔绘制
  ></android.gesture.GestureOverlayView>
</LinearLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <TextView
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:gravity="center"
   android:text="请输入手势名称" />
  <EditText  //输入手势的名称
   android:id="@ id/save_dialog_et"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />
 </LinearLayout>
 <ImageView  //展示绘制的手势
  android:id="@ id/save_dialog_iv"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
</LinearLayout>

1.4 代码部分:

package com.example.mygesture.addgesture;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.mygesture.R;
import com.example.mygesture.recognitiongesture.Main4Activity;
public class Main3Activity extends AppCompatActivity {
 GestureOverlayView gov;   //定义绘制组件
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main3);
  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
   ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);  
  }    //高版本需要动态申请权限
  init();
 }
 private void init() {
  gov = findViewById(R.id.activity_main3_gov);
//  gov.setGestureColor(Color.RED);  //设置绘制的颜色
  gov.setGestureStrokeWidth(4);  //设置画笔的宽度
  gov.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() { //设置绘制完成监听
   @Override
   public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {
    View saveDialog = getLayoutInflater().inflate(R.layout.save_dialog, null); //获取AlertDialog的布局样式
    final EditText editText = saveDialog.findViewById(R.id.save_dialog_et);
    ImageView imageView = saveDialog.findViewById(R.id.save_dialog_iv);
    Bitmap bitmap = gesture.toBitmap(128, 128, 10, 0xFFFF0000);  //将手势转换为位图
    imageView.setImageBitmap(bitmap);   //用ImageView加载手势图片
    new AlertDialog.Builder(Main3Activity.this).setView(saveDialog).setPositiveButton("确定", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      GestureLibrary gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/mygesture");//利用手势库获取存放手势文件的地址
      gestureLibrary.addGesture(editText.getText().toString(), gesture);  //向手势库中添加手势名称和手势
      gestureLibrary.save();    //保存手势库
      Toast.makeText(Main3Activity.this, "保存成功", Toast.LENGTH_SHORT).show();
     }
    }).setNegativeButton("取消", null)
      .show();
   }
  });
 }
 public void recognition(View view) {
  Intent intent = new Intent(this, Main4Activity.class);
  startActivity(intent);
 }
}

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

2.1 一样的先写布局文件

<?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"
 android:orientation="vertical"
 tools:context=".recognitiongesture.Main4Activity">

 <TextView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text="请绘制需要识别的手势" />

 <android.gesture.GestureOverlayView
  android:id="@ id/activity_main4_gov"
  android:layout_width="match_parent"
  android:layout_height="match_parent"></android.gesture.GestureOverlayView>
</LinearLayout>

2.2 代码的编写

package com.example.mygesture.recognitiongesture;
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.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.example.mygesture.R;
import java.util.ArrayList;
import java.util.logging.Level;
public class Main4Activity extends AppCompatActivity {
 GestureOverlayView gov;
 GestureLibrary gestureLibrary;  //定义手势库
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main4);
  init();
 }
 private void init() {
  gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/mygesture"); //获取手势文件
  if (gestureLibrary.load()) {   //判断手势文件是否存在以及加载
   Toast.makeText(this, "手势文件加载成功", Toast.LENGTH_SHORT).show();
  } else {
   Toast.makeText(this, "手势文件加载失败", Toast.LENGTH_SHORT).show();
  }
  gov = findViewById(R.id.activity_main4_gov);
  gov.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
   @Override
   public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = gestureLibrary.recognize(gesture); //匹配手势库中的所有手势
    ArrayList<String> result = new ArrayList<>();  //匹配结果数组
    for (Prediction pred : predictions) {
     if (pred.score > 2) {    //匹配手势库中的所有手势,并将相似度>2存入匹配结果数组
      result.add("相似度:"   pred.score);
     }
    }
    if (result.size() > 0) {  //这里用了适配器来作为AlertDialog的布局样式,用于显示所有手势的相似度
     ArrayAdapter<Object> arrayAdapter = new ArrayAdapter<Object>(Main4Activity.this, android.R.layout.simple_dropdown_item_1line, result.toArray());
     new AlertDialog.Builder(Main4Activity.this).setAdapter(arrayAdapter, null).setPositiveButton("确定", null).show();
    } else {
     Toast.makeText(Main4Activity.this, "未找到与之匹配的手势", Toast.LENGTH_SHORT).show();
    }
   }
  });
 }
}

总结

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

Android实现自定义手势和识别手势的功能的更多相关文章

  1. html5 canvas合成海报所遇问题及解决方案总结

    这篇文章主要介绍了html5 canvas合成海报所遇问题及解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Html5 video标签视频的最佳实践

    这篇文章主要介绍了Html5 video标签视频的最佳实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  3. HTML5在微信内置浏览器下右上角菜单的调整字体导致页面显示错乱的问题

    HTML5在微信内置浏览器下,在右上角菜单的调整字体导致页面显示错乱的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

  4. ios – containerURLForSecurityApplicationGroupIdentifier:在iPhone和Watch模拟器上给出不同的结果

    我使用默认的XCode模板创建了一个WatchKit应用程序.我向iOSTarget,WatchkitAppTarget和WatchkitAppExtensionTarget添加了应用程序组权利.(这是应用程序组名称:group.com.lombax.fiveminutes)然后,我尝试使用iOSApp和WatchKitExtension访问共享文件夹URL:延期:iOS应用:但是,测试NSURL

  5. Pan和2 Finger Pinch同步iOS – 同时 –

    2手势识别器:和:但同时捏和平底锅不起作用……

  6. ios – 视图的简单拖放?

    我正在学习iOS,但我找不到如何向UIView添加拖放行为.我试过了:它说“UIView没有可见的接口声明选择器addTarget”此外,我尝试添加平移手势识别器,但不确定这是否是我需要的它被称为,但不知道如何获得事件的坐标.在iOS中注册移动事件回调/拖放操作的标准简单方法是什么?

  7. ios – Swift 4添加手势:覆盖vs @objc

    我想在我的视图中添加一个手势,如下所示:但是,在Swift4中,我的编译器给出了以下错误:建议添加@objc以将此实例方法公开给Objective-C.实现此目的的另一个选项将覆盖touchesBegan()函数并使用它来处理点击.我试图以“Swift”的方式做到这一点,而不必带入Obj-C.有没有纯粹的Swift方式来添加这个轻击手势而不使用@objc?

  8. Ionic – Splash Screen适用于iOS,但不适用于Android

    我有一个离子应用程序,其中使用CLI命令离子资源生成的启动画面和图标iOS版本与正在渲染的启动画面完美配合,但在Android版本中,只有在加载应用程序时才会显示白屏.我检查了config.xml文件,所有路径看起来都是正确的,生成的图像出现在相应的文件夹中.(我使用了splash.psd模板来生成它们.我错过了什么?这是config.xml文件供参考,我觉得我在这里做错了–解决方法在config.xml中添加以下键:它对我有用!

  9. ios – UINavigationController自定义动画可防止滑动返回工作

    我注意到一些奇怪的东西,可能是UINavigationController中的一个错误.当你覆盖-navigationController:animationControllerForOperation:fromViewController:toViewController:并返回nil(对于默认的动画行为),拖放回去的手势不再有效.此方法的文档说明如果要使用标准导航控制器转换,则应返回“nil”

  10. ios – UITableView滑动手势与UITableViewCell滑动冲突

    以下是我编写的用于在UITableView上放置2个手指滑动的代码:我正在使用SWTableViewCell左右(单击)gestureRecognisers.当使用2个手指向左/向右滑动UITableView时,SWTableViewCell左右手势也会在此之后被触发.如何制止冲突?解决方法当您触摸SWTableViewCell时,将BOOLSWTableViewCellTouch设置为YES.

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部