Android實(shí)現(xiàn)Gesture手勢(shì)識(shí)別用法分析
本文實(shí)例分析了Android實(shí)現(xiàn)Gesture手勢(shì)識(shí)別用法。分享給大家供大家參考。具體如下:
很高興能在Android1.6的sdk看到手勢(shì)識(shí)別這一功能,之前一直在想,如何在android中實(shí)現(xiàn)nds游戲那樣用手勢(shì)(準(zhǔn)確點(diǎn)應(yīng)該是筆勢(shì))來(lái)控制游戲角色?現(xiàn)在總算看到一點(diǎn)曙光了,不過(guò)手勢(shì)要做到筆勢(shì)那樣隨心所欲地控制游戲人物,還有很多細(xì)節(jié)問(wèn)題需要處理。
在Android1.6的模擬器里面預(yù)裝了一個(gè)叫Gestures Builder的程序,這個(gè)程序就是讓你創(chuàng)建自己的手勢(shì)的(Gestures Builder的源代碼在sdk問(wèn)samples里面有,有興趣可以看看)。創(chuàng)建的手勢(shì)將被保存到/sdcard/gestures里面,把這個(gè)文件復(fù)制到你的工程/res/raw下,你就可以在你的工程里面使用這些手勢(shì)了。復(fù)制到/res/raw下的手勢(shì)是只讀的,也就是說(shuō)你不能修改或增加手勢(shì)了,如果想實(shí)現(xiàn)增改的話,可以直接加載sd卡里面的gestures文件。
在例子中,我創(chuàng)建了這樣的手勢(shì):

第二步:在layout里面創(chuàng)建GestureOverlayView,這個(gè)透明的view就是讓你在上面畫手勢(shì)用的,可以疊在其他View上面:
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <android.gesture.GestureOverlayView android:id="@+id/gestures" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1.0" /> </LinearLayout>
第三步:載入Gesture:
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mLibrary.load()) {
finish();
}
第四步:增加響應(yīng)函數(shù)OnGesturePerformedListener:
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures); gestures.addOnGesturePerformedListener(this);
以上四步就可以實(shí)現(xiàn)簡(jiǎn)單的Gesture識(shí)別原型了:
程序運(yùn)行結(jié)果如下,書寫一個(gè)a字,程序識(shí)別出,然后toast一個(gè)a出來(lái):

完整代碼如下:
package com.ray.test;
import java.util.ArrayList;
import android.app.Activity;
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.os.Bundle;
import android.widget.Toast;
public class TestGesture extends Activity implements OnGesturePerformedListener{
GestureLibrary mLibrary;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mLibrary.load()) {
finish();
}
}
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList predictions = mLibrary.recognize(gesture);
// We want at least one prediction
if (predictions.size() > 0) {
Prediction prediction = (Prediction) predictions.get(0);
// We want at least some confidence in the result
if (prediction.score > 1.0) {
// Show the spell
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
}
}
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android開(kāi)發(fā)之為activity增加左右手勢(shì)識(shí)別示例
- android使用gesturedetector手勢(shì)識(shí)別示例分享
- android創(chuàng)建手勢(shì)識(shí)別示例代碼
- Android應(yīng)用開(kāi)發(fā)中觸摸屏手勢(shì)識(shí)別的實(shí)現(xiàn)方法解析
- 理解Android的手勢(shì)識(shí)別提高APP的用戶體驗(yàn)
- Android View進(jìn)行手勢(shì)識(shí)別詳解
- 深入理解Android手勢(shì)識(shí)別
- Android基礎(chǔ)開(kāi)發(fā)之手勢(shì)識(shí)別
- 札記:android手勢(shì)識(shí)別功能實(shí)現(xiàn)(利用MotionEvent)
- Android手勢(shì)操作識(shí)別詳解
相關(guān)文章
Android Studio導(dǎo)入Project與Module的方法及實(shí)例
這篇文章主要介紹了Android Studio導(dǎo)入Project與Module的方法及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android apk安裝替換卸載廣播的實(shí)現(xiàn)代碼
以下代碼實(shí)現(xiàn)比較簡(jiǎn)單,根據(jù)接收到的Action來(lái)判斷應(yīng)用程序是安裝 卸載還是被替換成其他版本,需要的朋友可以參考下2013-07-07
Android仿Flipboard動(dòng)畫效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿Flipboard動(dòng)畫效果的實(shí)現(xiàn)代碼,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-01-01
Android Studio打包APK文件具體實(shí)現(xiàn)步驟解析
這篇文章主要介紹了Android Studio打包APK文件具體實(shí)現(xiàn)步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Android AlertDialog自定義樣式實(shí)現(xiàn)代碼
這篇文章主要介紹了Android AlertDialog自定義樣式實(shí)現(xiàn)代碼的相關(guān)資料,這里提供了實(shí)例代碼,一個(gè)簡(jiǎn)單示例,需要的朋友可以參考下2016-12-12
基于標(biāo)準(zhǔn)http實(shí)現(xiàn)Android多文件上傳
這篇文章主要介紹了基于標(biāo)準(zhǔn)http實(shí)現(xiàn)Android多文件上傳的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
android動(dòng)態(tài)壁紙調(diào)用的簡(jiǎn)單實(shí)例
動(dòng)態(tài)壁紙的實(shí)現(xiàn)其實(shí)就是在Activity中調(diào)用動(dòng)態(tài)壁紙服務(wù),通過(guò)綁定服務(wù)得到IWallpaperService,調(diào)用該接口中的attach函數(shù)實(shí)現(xiàn)壁紙的調(diào)用。2013-06-06
Android實(shí)現(xiàn)文字垂直滾動(dòng)、縱向走馬燈效果的實(shí)現(xiàn)方式匯總
本文給大家分享了三種方式實(shí)現(xiàn)Android文字垂直滾動(dòng)、縱向走馬燈效果,文中給大家介紹了相關(guān)屬性及注意事項(xiàng),需要的朋友參考下吧2017-12-12

