Android基礎(chǔ)開發(fā)之手勢識別
由于精確度等原因,手勢識別在android中用的并不多,不過這并不妨礙我們來玩玩這個神奇的玩意。
在android中要使用手勢,先得建立手勢庫,建立手勢庫非常簡單,新建一個android sample project,建一個android示例工程,然后選擇創(chuàng)建的android版本,完了之后看到這個界面:
選擇gesturebuilder,創(chuàng)建成功之后把它安裝到真機上,然后可以在里邊添加手勢,并給手勢命名。
創(chuàng)建完gesture之后,在eclipse的file explore窗口中查看系統(tǒng)文件,在sdcard文件夾中會多出一個gesture文件,先把這個文件導出到桌面。然后新建一個名叫g(shù)esture的工程,在res文件夾下新建一個raw文件夾,再把剛才的gesture文件拷貝進來,這樣我們這著工程就有了一個手勢庫了,下面看看怎么用這個手勢庫。
先看看布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.gesture.MainActivity" > <android.gesture.GestureOverlayView android:id="@+id/mygesture" android:layout_width="match_parent" android:layout_height="match_parent" android:gestureStrokeType="multiple" /> </RelativeLayout>
布局文件中就一個關(guān)于gestureOverlayView的控件,android:gestureStrokeType屬性有兩個值,一個是multiple,另一個是single,multiple表示支持多筆畫,single表示支持單筆畫。
Java代碼:
public class MainActivity extends Activity { private GestureOverlayView myges; private GestureLibrary library; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myges = (GestureOverlayView) this.findViewById(R.id.mygesture); library = GestureLibraries.fromRawResource(this, R.raw.gestures); // 讀取庫中數(shù)據(jù) library.load(); //監(jiān)聽繪制手勢事件 myges.addOnGesturePerformedListener(new OnGesturePerformedListener() { @Override public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { //Prediction中存儲的是比對的結(jié)果 List<Prediction> list = library.recognize(gesture); //遍歷結(jié)果,score是比對后的分數(shù),分數(shù)越大,越相近 for(Prediction p : list){ Log.i("lenve", p.name+"------------"+p.score); } if(list.get(0).score>4){ Toast.makeText(MainActivity.this,list.get(0).name, Toast.LENGTH_LONG).show(); }else{ Toast.makeText(MainActivity.this,"手勢無法識別", Toast.LENGTH_LONG).show(); } } }); } }
關(guān)鍵代碼已注釋。就這么簡單,由于識別率等問題,手勢識別目前用的并不多。
原文鏈接:http://blog.csdn.net/u012702547/article/details/45727729
源碼下載:Android開發(fā)之手勢識別
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android通過滑動實現(xiàn)Activity跳轉(zhuǎn)(手勢識別器應用)
- Android手勢識別器GestureDetector使用詳解
- 札記:android手勢識別功能實現(xiàn)(利用MotionEvent)
- Android View進行手勢識別詳解
- Android應用開發(fā)中觸摸屏手勢識別的實現(xiàn)方法解析
- android開發(fā)之為activity增加左右手勢識別示例
- android創(chuàng)建手勢識別示例代碼
- android使用gesturedetector手勢識別示例分享
- 理解Android的手勢識別提高APP的用戶體驗
- Android使用GestureOverlayView控件實現(xiàn)手勢識別
相關(guān)文章
Android編程開發(fā)之NotiFication用法詳解
這篇文章主要介紹了Android編程開發(fā)之NotiFication用法,結(jié)合實例形式較為詳細的分析了NotiFication的功能、使用技巧與注意事項,需要的朋友可以參考下2015-12-12Android使用Sensor感應器實現(xiàn)線程中刷新UI創(chuàng)建android測力計的功能
這篇文章主要介紹了Android使用Sensor感應器實現(xiàn)線程中刷新UI創(chuàng)建android測力計的功能,實例分析了Android使用Sensor感應器實現(xiàn)UI刷新及創(chuàng)建測力器的技巧,需要的朋友可以參考下2015-12-12android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode
這篇文章主要介紹了android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode,有需要的可以了解一下。2016-11-11