這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Android中怎么自定義手勢和識別手勢功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
和平網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站開發(fā)等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)建站成立于2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站。
1. 先完成自定義手勢的Activity
1.1 因為需要存儲手勢文件所以需要聲明權(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 簡單寫一個布局文件,其中用到了GestureOverlayView,相當(dāng)于一個繪制組件。其中有一個重要屬性gestureStrokeType,值為single時表示只繪制一筆,若要多筆繪制值應(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="識別手勢" /> <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); } //高版本需要動態(tài)申請權(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); //將手勢轉(zhuǎn)換為位圖 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<>(); //匹配結(jié)果數(shù)組 for (Prediction pred : predictions) { if (pred.score > 2) { //匹配手勢庫中的所有手勢,并將相似度>2存入匹配結(jié)果數(shù)組 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中怎么自定義手勢和識別手勢功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章名稱:Android中怎么自定義手勢和識別手勢功能
文章源于:http://jinyejixie.com/article10/iepgdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站排名、App開發(fā)、自適應(yīng)網(wǎng)站、微信公眾號、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)