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

下面是運行界面

產(chǎn)生的手勢庫,默認存放在sd卡的根目錄下面,我們將生成的手勢庫文件放在我們的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>
布局很簡單,就是一個用于接受手勢的控件,下面是邏輯代碼的實現(xiàn)
public class MainActivity extends Activity {
private boolean success;
// 定義手勢庫
private GestureLibrary library;
private GestureOverlayView gestureView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 找到手勢庫
library = GestureLibraries.fromRawResource(this, R.raw.gestures);
// 加載手勢庫
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) {
// 如果手勢庫加載成功
if (success) {
// 從手勢庫中查找匹配的手勢,最匹配的記錄會放在最前面
ArrayList<Prediction> predictions = library.recognize(gesture);
if (!predictions.isEmpty()) {
// 獲取第一個匹配的手勢
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)過這幾個步驟,我們就實現(xiàn)了最簡單的手勢識別的功能了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android通過滑動實現(xiàn)Activity跳轉(zhuǎn)(手勢識別器應(yīng)用)
- Android手勢識別器GestureDetector使用詳解
- 札記:android手勢識別功能實現(xiàn)(利用MotionEvent)
- Android View進行手勢識別詳解
- Android基礎(chǔ)開發(fā)之手勢識別
- Android應(yīng)用開發(fā)中觸摸屏手勢識別的實現(xiàn)方法解析
- android開發(fā)之為activity增加左右手勢識別示例
- android創(chuàng)建手勢識別示例代碼
- android使用gesturedetector手勢識別示例分享
- 理解Android的手勢識別提高APP的用戶體驗
相關(guān)文章
PowerManagerService之喚醒鎖的使用獲取創(chuàng)建示例解析
這篇文章主要為大家介紹了PowerManagerService之喚醒鎖的使用獲取創(chuàng)建示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
RecyclerView仿應(yīng)用列表實現(xiàn)網(wǎng)格布局
這篇文章主要為大家詳細介紹了RecyclerView仿應(yīng)用列表實現(xiàn)網(wǎng)格布局,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09
Android 老生常談LayoutInflater的新認知
今天不想去聊一些Android的新功能,新特性之類的東西,特別想聊一聊這個老生常談的話題:LayoutInflater,感興趣的朋友來看看吧2022-03-03
Android App中ViewPager與Fragment結(jié)合的一些問題解決
這篇文章主要介紹了Android App中ViewPager與Fragment結(jié)合的一些問題解決,重點講解了如何更新及替換ViewPager中的Fragment,需要的朋友可以參考下2016-03-03
Android開發(fā)實現(xiàn)切換主題及換膚功能示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)切換主題及換膚功能,涉及Android界面布局與樣式動態(tài)操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-03-03
Android自定義view系列之99.99%實現(xiàn)QQ側(cè)滑刪除效果實例代碼詳解
這篇文章給大家介紹android自定義view系列之99.99%實現(xiàn)QQ側(cè)滑刪除效果,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友參考下吧2016-09-09

