Android實(shí)現(xiàn)自定義手勢(shì)和識(shí)別手勢(shì)的功能
1. 先完成自定義手勢(shì)的Activity
1.1 因?yàn)樾枰鎯?chǔ)手勢(shì)文件所以需要聲明權(quán)限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> //讀取SD卡權(quán)限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> //寫入SD卡權(quán)限
1.2 簡(jiǎn)單寫一個(gè)布局文件,其中用到了GestureOverlayView,相當(dāng)于一個(gè)繪制組件。其中有一個(gè)重要屬性gestureStrokeType,值為single時(shí)表示只繪制一筆,若要多筆繪制值應(yīng)該設(shè)為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="識(shí)別手勢(shì)" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="請(qǐng)繪制手勢(shì)" /> <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="請(qǐng)輸入手勢(shì)名稱" /> <EditText //輸入手勢(shì)的名稱 android:id="@+id/save_dialog_et" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <ImageView //展示繪制的手勢(shì) 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);
} //高版本需要?jiǎng)討B(tài)申請(qǐng)權(quán)限
init();
}
private void init() {
gov = findViewById(R.id.activity_main3_gov);
// gov.setGestureColor(Color.RED); //設(shè)置繪制的顏色
gov.setGestureStrokeWidth(4); //設(shè)置畫筆的寬度
gov.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() { //設(shè)置繪制完成監(jiān)聽
@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); //將手勢(shì)轉(zhuǎn)換為位圖
imageView.setImageBitmap(bitmap); //用ImageView加載手勢(shì)圖片
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");//利用手勢(shì)庫(kù)獲取存放手勢(shì)文件的地址
gestureLibrary.addGesture(editText.getText().toString(), gesture); //向手勢(shì)庫(kù)中添加手勢(shì)名稱和手勢(shì)
gestureLibrary.save(); //保存手勢(shì)庫(kù)
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. 接下來完成識(shí)別手勢(shì)的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="請(qǐng)繪制需要識(shí)別的手勢(shì)" /> <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; //定義手勢(shì)庫(kù)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
init();
}
private void init() {
gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/mygesture"); //獲取手勢(shì)文件
if (gestureLibrary.load()) { //判斷手勢(shì)文件是否存在以及加載
Toast.makeText(this, "手勢(shì)文件加載成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "手勢(shì)文件加載失敗", 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); //匹配手勢(shì)庫(kù)中的所有手勢(shì)
ArrayList<String> result = new ArrayList<>(); //匹配結(jié)果數(shù)組
for (Prediction pred : predictions) {
if (pred.score > 2) { //匹配手勢(shì)庫(kù)中的所有手勢(shì),并將相似度>2存入匹配結(jié)果數(shù)組
result.add("相似度:" + pred.score);
}
}
if (result.size() > 0) { //這里用了適配器來作為AlertDialog的布局樣式,用于顯示所有手勢(shì)的相似度
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, "未找到與之匹配的手勢(shì)", Toast.LENGTH_SHORT).show();
}
}
});
}
}
總結(jié)
以上所述是小編給大家介紹的Android實(shí)現(xiàn)自定義手勢(shì)和識(shí)別手勢(shì)的功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
上一篇:Android屏幕旋轉(zhuǎn)之橫屏豎屏切換的實(shí)現(xiàn)
欄 目:Android
下一篇:Android 獲取 usb 權(quán)限的兩種方法
本文標(biāo)題:Android實(shí)現(xiàn)自定義手勢(shì)和識(shí)別手勢(shì)的功能
本文地址:http://www.jygsgssxh.com/a1/Android/9157.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?


