手勢互動之GestureOverlayView,gestureoverlayview

來源:互聯網
上載者:User

手勢互動之GestureOverlayView,gestureoverlayview

一種用於手勢輸入的透明覆蓋層,可以覆蓋在其他空間的上方,也可包含在其他控制項 android.gesture.GestureOverlayView

獲得手勢檔案

需要用GesturesBuilder,如果沒有就安裝這個app

安裝

其實這是一個Sample的,用eclipse和android studio中各有不同方法匯入,在版本android2.33中有

取得檔案

在GesturesBuilder畫對應的手勢和命名之後,可以在檔案夾mmt中的sdcard中有個gestures檔案,把這個檔案儲存起來,匯入到你的項目的res檔案夾中

監聽器

有三種監聽器

  1. OnGestureListener 手勢監聽器
  2. OnGesturePerformedListener 手勢執行監聽器(一般用這個)
  3. OnGesturingListener執行中的監聽器
例子

實現在一張圖片上各種自訂手勢的效果

1.布局

可以在面板中的Advanced有GestureOverlayView,或者直接在AndroidMainFest.XML編寫

<android.gesture.GestureOverlayView        android:id="@+id/gestureOverlayView"        android:layout_width="180dp"        android:layout_height="180dp" >        <ImageView             android:layout_width="match_parent"            android:layout_height="match_parent"            android:src="@drawable/ic_launcher"/    </android.gesture.GestureOverlayView>
2.找到並載入剛剛匯出來的手勢資源

我的是res檔案夾下建立的raw檔案夾。設定了這三個手勢

//這裡用到final是由於匿名類(內部類)用到外部類,涉及到生命週期的緣故final GestureLibrary library =GestureLibraries.fromRawResource(MainActivity.this, R.raw.gestures);library.load();
3.給gestureOverlayView設定監聽器
gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {@Overridepublic void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {// TODO Auto-generated method stub//識別手勢,傳回值是一個手勢的集合ArrayList<Prediction> mygesture = library.recognize(gesture);Prediction prediction = mygesture.get(0);/*
*score是指相似性,就是有點類似在這個誤差範圍內都認為是這個手勢 *至於這個的數值需要自己調試,找一個合適的值,值越大,就越要接近原來的 * name就是你的手勢的名字 */if (prediction.score >= 5.0 ) {if (prediction.name.equals("exit")) {finish();}else if(prediction.name.equals("next")){Toast.makeText(MainActivity.this, "下一個", Toast.LENGTH_SHORT).show();}else if (prediction.name.equals("previous")) {Toast.makeText(MainActivity.this, "前一個", Toast.LENGTH_SHORT).show();}}else {Toast.makeText(MainActivity.this, "沒有這個手勢", Toast.LENGTH_SHORT).show();}}});
效果

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.