標籤:
目標效果:
程式運行,畫左箭頭提示上一個,畫右箭頭提示下一個,並且還可以畫符號退出程式。
這裡自訂手勢使用的是GestureOverlayView進行設定的,SDK2.0以上系統都內建了一個GestureBuilder手勢庫,SDK4.2以前路徑是android-sdk-windows\samples\android-10\GestureBuilder,4.2以後路徑是sdk-extras-android-support-samples-GestureBuilder,有時可能自己安裝的並沒有這個庫,那就需要下載一個放到路徑的目錄中,http://pan.baidu.com/s/1bpbno6r。
1.首先需要匯入手勢庫,添加手勢檔案,模擬器上有現成的軟體這一步可以省略,但是真機測試時需要匯入在手機上運行,因為我之前置入一次了,所以下邊時提示紅色錯號和Finish不能點擊,第一次匯入都是正常的。(沒有圖示的直接點擊next)
2.運行後,模擬器或手機上會多了一個小程式。
3.開啟後添加項目需要的手勢,儲存後三,提示儲存路徑,模擬器儲存路徑為storage-sdcard-gestures,真機儲存路徑為storage-emulated-0-gestures。(可能不同手機路徑不同)
4.現在只需要找到gestures檔案,開啟File Explorer,圖一為模擬器的檔案路徑storage-sdcard-gestures,圖二為真機的檔案路徑mnt-shell-emulated-0-gestures。(暫時不太明白為什麼為什麼真機的路徑和儲存時提示的不太一樣)
5.匯入gestures檔案到案頭,建立項目,在res檔案夾下建立raw檔案夾,將gestures檔案複製到raw檔案夾中。
6.activity_main.xml頁面放置一個ImageView控制項和GestureOverlayView控制項,並且使用GestureOverlayView控制項將ImageView控制項包含起來。(不包含也可以,GestureOverlayView控制項預設在所有控制項上方)
activity_main.xml頁面:
<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=".MainActivity" > <!-- Android:eventsInterceptionEnabled 定義當手勢已經被識別出來時,是否攔截該手勢 Android:fadeDuration 當使用者畫完,手勢效果淡出的時間 Android:fadeEnabled 使用者畫完之後,手勢是否自動淡出 Android:gestureColor 手勢的顏色 Android:gestureStrokeType 筆畫的類型 Android:geatureStrokeWidth 筆畫的粗細 --> <android.gesture.GestureOverlayView android:gestureColor="#ff0000" android:gestureStrokeWidth="10" android:id="@+id/gestureOverlayView" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/ivShow" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/ic_launcher" /> </android.gesture.GestureOverlayView></RelativeLayout>
7.MainActivity.java頁面匹配識別手勢。MainActivity.java頁面:
package com.example.gestureoverlayview;import java.util.ArrayList;import android.os.Bundle;import android.app.Activity;import android.gesture.Gesture;import android.gesture.GestureLibraries;import android.gesture.GestureLibrary;import android.gesture.GestureOverlayView;import android.gesture.GestureOverlayView.OnGesturePerformedListener;import android.gesture.Prediction;import android.view.Menu;import android.widget.Toast;public class MainActivity extends Activity {private GestureOverlayView gestureOverlayView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);gestureOverlayView=(GestureOverlayView) findViewById(R.id.gestureOverlayView);/*找到預設定的手勢檔案並載入進來*/final GestureLibrary library=GestureLibraries.fromRawResource(MainActivity.this,R.raw.gestures);//擷取手勢檔案library.load();/*匹配識別*/gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {@Overridepublic void onGesturePerformed(GestureOverlayView arg0, Gesture gesture) {//讀出手勢庫中內容 識別手勢ArrayList<Prediction> mygesture=library.recognize(gesture);Prediction predction=mygesture.get(0);if(predction.score>=4.0){//相似性大於某個值(數字越大代表要求越相似),說明有該手勢if(predction.name.equals("exit")){finish();}else if(predction.name.equals("next")){Toast.makeText(MainActivity.this,"下一個",Toast.LENGTH_SHORT).show();}else if(predction.name.equals("previous")){Toast.makeText(MainActivity.this,"上一個",Toast.LENGTH_SHORT).show();}}else{Toast.makeText(MainActivity.this,"沒有該手勢",Toast.LENGTH_SHORT).show();}}});}}
8.運行就可以顯示目標效果了。
Android-GestureOverlayView自訂手勢命令