android 手势识别代码
生活随笔
收集整理的這篇文章主要介紹了
android 手势识别代码
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
手勢(shì)創(chuàng)建主要用到GestureOverlayView和GestureLibrary。GestureOverlayView的父類(lèi),GestureLibrary類(lèi)主要對(duì)手勢(shì)進(jìn)行保存、刪除等操作的,存放手勢(shì)的倉(cāng)庫(kù)。
工程目錄:
1、首先先生成手勢(shì)庫(kù),自己繪制喜歡的手勢(shì),然后命名好保存。詳細(xì)代碼如下
package com.example.gesture; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.gesture.Gesture; import android.gesture.GestureLibraries; import android.gesture.GestureLibrary; import android.gesture.GestureOverlayView; import android.gesture.GestureOverlayView.OnGesturePerformedListener; import android.graphics.Bitmap; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast;public class drawActivity extends Activity {private GestureOverlayView gestureView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.drawgesture);initUI();}private void initUI() {// TODO Auto-generated method stubgestureView = (GestureOverlayView) findViewById(R.id.gesture);// 設(shè)置手勢(shì)的繪制顏色 gestureView.setGestureColor(Color.RED); // 設(shè)置手勢(shì)的繪制寬度 gestureView.setGestureStrokeWidth(5);// gestureView.setGestureColor(0xff009966);//設(shè)置畫(huà)筆顏色// gestureView.setFadeOffset(2000);//兩個(gè)筆畫(huà)之間的時(shí)間間隔// 為gesture的手勢(shì)完成事件綁定事件監(jiān)聽(tīng)器 gestureView.addOnGesturePerformedListener(new OnGesturePerformedListener() {@Overridepublic void onGesturePerformed(GestureOverlayView overlay,final Gesture gesture) {// TODO Auto-generated method stubAlertDialog.Builder builder = new AlertDialog.Builder(drawActivity.this);LayoutInflater inflater = getLayoutInflater();final View layout = inflater.inflate(R.layout.savegesture, null);ImageView image = (ImageView)layout.findViewById(R.id.show);// 根據(jù)Gesture包含的手勢(shì)創(chuàng)建一個(gè)位圖 Bitmap bitmap = gesture.toBitmap(128, 128, 10, 0xFFFF0000); image.setImageBitmap(bitmap); builder.setView(layout);builder.setIcon(R.drawable.ic_launcher);builder.setTitle(R.string.title); builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubEditText edit = (EditText)layout.findViewById(R.id.gesture_name); // 獲取指定文件對(duì)應(yīng)的手勢(shì)庫(kù) GestureLibrary gestureLib = GestureLibraries .fromFile("/sdcard/gestures"); // 添加手勢(shì) gestureLib.addGesture(edit.getText().toString(),gesture); // 保存手勢(shì)庫(kù) gestureLib.save(); Toast.makeText(getApplicationContext(), "手勢(shì)保存成功!", Toast.LENGTH_SHORT).show();}});builder.setNeutralButton("cancel", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stub }});final AlertDialog dlg = builder.create();dlg.show();}});}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();}} 代碼布局為:
<?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" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content"android:gravity="center_horizontal" android:text="@string/ad"/> <!-- 使用手勢(shì)繪制組件 --> <android.gesture.GestureOverlayView android:id="@+id/gesture" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gestureStrokeType="multiple" /> </LinearLayout>
保存手勢(shì)布局自定義dialog布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dip" android:text="@string/gesture_name" /> <!-- 定義一個(gè)文本框來(lái)讓用戶(hù)輸入手勢(shì)名 --> <EditText android:id="@+id/gesture_name" android:inputType="none"android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> <!-- 定義一個(gè)圖片框來(lái)顯示手勢(shì) --> <ImageView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" /> </LinearLayout> 最后還需要添加權(quán)限:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>2、生成的手勢(shì)庫(kù)保存路基可自己定義,gestures為手勢(shì)庫(kù)的名字
GestureLibrary gestureLib = GestureLibraries.fromFile("/sdcard/gestures"); 3、手勢(shì)的使用,在res目錄下新建一個(gè)raw文件,然后添加手勢(shì)庫(kù)進(jìn)來(lái)方便調(diào)用,首先是先獲取手勢(shì)庫(kù),然后對(duì)手勢(shì)的相似度進(jìn)行判斷,可設(shè)置大小。最后對(duì)之前定義名手勢(shì)名字進(jìn)行判斷,再執(zhí)行自己想操作的。詳細(xì)代碼:
package com.example.gesture;import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.gesture.Gesture; import android.gesture.GestureLibraries; import android.gesture.GestureLibrary; import android.gesture.GestureOverlayView; import android.gesture.Prediction; import android.gesture.GestureOverlayView.OnGesturePerformedListener; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.Toast;public class useActivity extends Activity {GestureOverlayView gestureView;GestureLibrary gLibrary;boolean loadState;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.usegesture);initUI();}private void initUI() {// TODO Auto-generated method stubgestureView = (GestureOverlayView) this.findViewById(R.id.myGesture);gestureView.addOnGesturePerformedListener(new MyOnGesturePerformedListener());// 創(chuàng)建手勢(shì)庫(kù)對(duì)象GestureLibrarygLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);// 加載手勢(shì)庫(kù)資源loadState = gLibrary.load();}private final class MyOnGesturePerformedListener implementsOnGesturePerformedListener {public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {if (loadState) {//加載手勢(shì)資源成功// 獲取畫(huà)的圖形進(jìn)行匹配,匹配程度就是Prediction中的scoreArrayList<Prediction> predictions = gLibrary.recognize(gesture); if (!predictions.isEmpty()) {// 如果用戶(hù)畫(huà)了圖形,就會(huì)匹配Prediction prediction = predictions.get(0);Log.i("TAG", String.valueOf(prediction.score));//prediction的score屬性代表了與手勢(shì)的相似程度//prediction的name代表手勢(shì)對(duì)應(yīng)的字母//這里要求匹配度要在30%以上if (prediction.score > 3) {// 判斷相似度大于3,與里面的兩者進(jìn)行匹配 if ("2".equals(prediction.name)) {//打電話(huà)Toast.makeText(getApplicationContext(), "手勢(shì)識(shí)別成功!", Toast.LENGTH_LONG).show();Intent intent1 = new Intent(Intent.ACTION_CALL,Uri.parse("tel:668301"));startActivity(intent1); }} else {// 相似度小于1,不識(shí)別Toast.makeText(getApplicationContext(), "手勢(shì)識(shí)別率太低,請(qǐng)重新輸入!", Toast.LENGTH_LONG).show();//手勢(shì)識(shí)別率低,請(qǐng)重新輸入}//判斷識(shí)別度在百分之20以上if(prediction.score >2){if("3".equals(prediction.name)){Toast.makeText(getApplicationContext(), "手勢(shì)識(shí)別成功!", Toast.LENGTH_LONG).show();//showToast(R.string.send);Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:668301"));startActivity(intent);}}else {// 相似度小于1,不識(shí)別Toast.makeText(getApplicationContext(), "手勢(shì)識(shí)別率太低,請(qǐng)重新輸入!", Toast.LENGTH_LONG).show();//手勢(shì)識(shí)別率低,請(qǐng)重新輸入} } else {//沒(méi)有畫(huà)圖形Toast.makeText(getApplicationContext(), "沒(méi)有手勢(shì)", Toast.LENGTH_LONG).show();}} else {Toast.makeText(getApplicationContext(), "手勢(shì)庫(kù)沒(méi)有加載成功", Toast.LENGTH_LONG).show();}}} }實(shí)現(xiàn)的布局代碼:
<?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" ><TextViewandroid:layout_width="fill_parent"android:gravity="center_horizontal"android:textSize="20sp"android:layout_height="wrap_content"android:text="@string/hello_world" /><android.gesture.GestureOverlayView android:id="@+id/myGesture"android:layout_width="fill_parent"android:layout_height="0dip"android:layout_weight="1.0"/> </LinearLayout> 完整的權(quán)限代碼:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.gesture"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="18" /><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.CALL_PHONE"/> <applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.gesture.MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".drawActivity"></activity><activity android:name=".useActivity"></activity></application></manifest>
詳細(xì)代碼下載地址: 點(diǎn)擊打開(kāi)鏈接
總結(jié)
以上是生活随笔為你收集整理的android 手势识别代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Unity游戏开发——新发教你做游戏(三
- 下一篇: Premiere视频片段剪辑、添加音乐、