Android使用GestureOverlayView控件實(shí)現(xiàn)手勢(shì)識(shí)別
在Android開發(fā)中,我們不光可以使用已有的實(shí)現(xiàn)方式,而且,我們還可以利用Android這個(gè)智能手機(jī)平臺(tái),實(shí)現(xiàn)一些比較有特色的功能。本篇文章介紹使用GestureOverlayView這個(gè)控件,實(shí)現(xiàn)簡(jiǎn)單的手勢(shì)識(shí)別的小例子。
首先,在使用手勢(shì)識(shí)別之前,我們需要建立一個(gè)手勢(shì)庫(kù),創(chuàng)建手勢(shì)庫(kù),我們可以找到sdk自帶的實(shí)例程序,比如我本地的路徑為sdk\samples\android-18\input\gestures,找到這個(gè)程序,然后建立一個(gè)新項(xiàng)目,將其整合之后,就可以用于產(chǎn)生手勢(shì)庫(kù)。
整合之后的項(xiàng)目結(jié)構(gòu)如下

下面是運(yùn)行界面

產(chǎn)生的手勢(shì)庫(kù),默認(rèn)存放在sd卡的根目錄下面,我們將生成的手勢(shì)庫(kù)文件放在我們的raw目錄下面

下面,我們就開始寫布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.gesture.GestureOverlayView android:id="@+id/gestures" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1.0" android:gestureStrokeType="multiple" /> </LinearLayout>
布局很簡(jiǎn)單,就是一個(gè)用于接受手勢(shì)的控件,下面是邏輯代碼的實(shí)現(xiàn)
public class MainActivity extends Activity {
private boolean success;
// 定義手勢(shì)庫(kù)
private GestureLibrary library;
private GestureOverlayView gestureView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 找到手勢(shì)庫(kù)
library = GestureLibraries.fromRawResource(this, R.raw.gestures);
// 加載手勢(shì)庫(kù)
success = library.load();
gestureView = (GestureOverlayView) this.findViewById(R.id.gestures);
// 添加事件監(jiān)聽器
gestureView.addOnGesturePerformedListener(new GestureListener());
}
private final class GestureListener implements OnGesturePerformedListener {
@Override
public void onGesturePerformed(GestureOverlayView overlay,
Gesture gesture) {
// 如果手勢(shì)庫(kù)加載成功
if (success) {
// 從手勢(shì)庫(kù)中查找匹配的手勢(shì),最匹配的記錄會(huì)放在最前面
ArrayList<Prediction> predictions = library.recognize(gesture);
if (!predictions.isEmpty()) {
// 獲取第一個(gè)匹配的手勢(shì)
Prediction prediction = predictions.get(0);
// 如果匹配度>30%,就執(zhí)行下面的操作
if (prediction.score > 3) {
// 關(guān)閉應(yīng)用
if ("agree".equals(prediction.name)) {
android.os.Process.killProcess(android.os.Process
.myPid());
// 撥打電話
} else if ("5556".equals(prediction.name)) {
Intent intent = new Intent(Intent.ACTION_CALL,
Uri.parse("tel:5556"));
startActivity(intent);
}
}
}
}
}
}
}
經(jīng)過這幾個(gè)步驟,我們就實(shí)現(xiàn)了最簡(jiǎn)單的手勢(shì)識(shí)別的功能了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android通過滑動(dòng)實(shí)現(xiàn)Activity跳轉(zhuǎn)(手勢(shì)識(shí)別器應(yīng)用)
- Android手勢(shì)識(shí)別器GestureDetector使用詳解
- 札記:android手勢(shì)識(shí)別功能實(shí)現(xiàn)(利用MotionEvent)
- Android View進(jìn)行手勢(shì)識(shí)別詳解
- Android基礎(chǔ)開發(fā)之手勢(shì)識(shí)別
- Android應(yīng)用開發(fā)中觸摸屏手勢(shì)識(shí)別的實(shí)現(xiàn)方法解析
- android開發(fā)之為activity增加左右手勢(shì)識(shí)別示例
- android創(chuàng)建手勢(shì)識(shí)別示例代碼
- android使用gesturedetector手勢(shì)識(shí)別示例分享
- 理解Android的手勢(shì)識(shí)別提高APP的用戶體驗(yàn)
相關(guān)文章
PowerManagerService之喚醒鎖的使用獲取創(chuàng)建示例解析
這篇文章主要為大家介紹了PowerManagerService之喚醒鎖的使用獲取創(chuàng)建示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局
這篇文章主要為大家詳細(xì)介紹了RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Android 老生常談LayoutInflater的新認(rèn)知
今天不想去聊一些Android的新功能,新特性之類的東西,特別想聊一聊這個(gè)老生常談的話題:LayoutInflater,感興趣的朋友來看看吧2022-03-03
Android App中ViewPager與Fragment結(jié)合的一些問題解決
這篇文章主要介紹了Android App中ViewPager與Fragment結(jié)合的一些問題解決,重點(diǎn)講解了如何更新及替換ViewPager中的Fragment,需要的朋友可以參考下2016-03-03
Android開發(fā)實(shí)現(xiàn)切換主題及換膚功能示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)切換主題及換膚功能,涉及Android界面布局與樣式動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03
Android自定義view系列之99.99%實(shí)現(xiàn)QQ側(cè)滑刪除效果實(shí)例代碼詳解
這篇文章給大家介紹android自定義view系列之99.99%實(shí)現(xiàn)QQ側(cè)滑刪除效果,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-09-09

