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

Android实现自定义手势和识别手势的功能,第1张

概述Android实现自定义手势识别手势的功能 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. 先完成自定义手势的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实现自定义手势和识别手势的功能全部内容,希望文章能够帮你解决Android实现自定义手势和识别手势的功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1145809.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存