Android实现自定义手势和识别手势的功能

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

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实现自定义手势和识别手势的功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

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

Android框架学习之Volley和Glide详解

这篇文章主要给大家介绍了关于Android框架学习之Volley和Glide的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android中Fragment的基本用法示例总结

Fragment是activity的界面中的一部分或一种行为,下面这篇文章主要给大家介绍了关于Android中Fragment的基本用法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

Android.mk引入第三方jar包和so库文件的方法

这篇文章主要介绍了Android.mk引入第三方jar包和so库文件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿微信录制小视频

这篇文章主要为大家详细介绍了Android仿微信录制小视频,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

android实现一键锁屏和一键卸载的方法实例

这篇文章主要给大家介绍了关于android如何实现一键锁屏和一键卸载的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

Android手势密码--设置和校验功能的实现代码

这篇文章主要介绍了Android手势密码--设置和校验功能的实现代码,非常不错,具有一定的参考校验价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Kotlin学习笔记之const val与val

这篇文章主要给大家介绍了关于Kotlin学习笔记之const val与val的相关资料,并给大家介绍了const val和val区别以及Kotlin中var和val的区别,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android实现调用系统分享功能示例的总结

这篇文章主要介绍了通过Android调用系统分享文本信息、单张图片、多个文件和指定分享到微信、QQ,同时分享图片和文字的功能示例,小编觉得挺不错,一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android自定义view实现输入控件

这篇文章主要为大家详细介绍了Android自定义view实现输入控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码

这篇文章主要介绍了Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码的实例代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多