Android基礎開發之手勢識別_Android

來源:互聯網
上載者:User

由於精確度等原因,手勢識別在android中用的並不多,不過這並不妨礙我們來玩玩這個神奇的玩意。

在android中要使用手勢,先得建立手勢庫,建立手勢庫非常簡單,建立一個android sample project,建一個android樣本工程,然後選擇建立的android版本,完了之後看到這個介面:

選擇gesturebuilder,建立成功之後把它安裝到真機上,然後可以在裡邊添加手勢,並給手勢命名。

建立完gesture之後,在eclipse的file explore視窗中查看系統檔案,在sdcard檔案夾中會多出一個gesture檔案,先把這個檔案匯出到案頭。然後建立一個名叫gesture的工程,在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>

布局檔案中就一個關於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); // 讀取庫中資料 library.load(); //監聽繪製手勢事件 myges.addOnGesturePerformedListener(new OnGesturePerformedListener() {  @Override  public void onGesturePerformed(GestureOverlayView overlay,   Gesture gesture) {  //Prediction中儲存的是比對的結果  List<Prediction> list = library.recognize(gesture);  //遍曆結果,score是比對後的分數,分數越大,越相近  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();  }  } }); }}

關鍵代碼已注釋。就這麼簡單,由於識別率等問題,手勢識別目前用的並不多。

原文連結:http://blog.csdn.net/u012702547/article/details/45727729

源碼下載:Android開發之手勢識別

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.