標籤:
在Android1.6的模擬器裡面預裝了一個叫Gestures Builder的程式,這個程式就是讓你建立自己的手勢的(Gestures Builder的原始碼在sdk問samples裡面有,有興趣可以看看)
將上面這四個檔案複製到你的工程目錄下面,
在模擬器上面運行這個工程檔案,在模擬器上面建立一些手勢檔案,例如:
建立的手勢將被儲存到/mnt/sdcard/gestures裡面,然後建立一個測試的手勢專案檔,將gestures檔案複製到res目錄中的raw檔案下面,
然後配置xml檔案,xml配置如下:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.xunfang.gesture.MainActivity" >10 11 <android.gesture.GestureOverlayView 12 android:id="@+id/gv"13 android:layout_width="match_parent"14 android:layout_height="match_parent"15 android:background="#000000"16 android:gestureStrokeWidth="10"17 android:gestureColor="#ff0000"18 />19 20 21 </RelativeLayout>
GestureOverlayView:一種用於手勢輸入的透明覆蓋層,可覆蓋在其他控制項的上方,也可包含其他控制項。
Android:gestureStrokeType 定義筆畫(定義為手勢)的類型
Android:gestureStrokeWidth 畫手勢時,筆劃的寬度
activity檔案內容如下
1 package com.xunfang.gesture; 2 3 import java.util.ArrayList; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.gesture.Gesture; 8 import android.gesture.GestureLibraries; 9 import android.gesture.GestureLibrary;10 import android.gesture.GestureOverlayView;11 import android.gesture.Prediction;12 import android.net.Uri;13 import android.os.Bundle;14 import android.view.MotionEvent;15 import android.widget.Toast;16 17 public class MainActivity extends Activity {18 19 private GestureOverlayView gv ;20 21 private boolean loadStatus ;22 23 private GestureLibrary gestureLibrary ;24 @Override25 protected void onCreate(Bundle savedInstanceState) {26 super.onCreate(savedInstanceState);27 setContentView(R.layout.activity_main);28 29 //拿到控制項30 gv = (GestureOverlayView) findViewById(R.id.gv) ;31 32 //建立載入手勢庫的工具33 gestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures) ;34 //載入手勢庫35 loadStatus = gestureLibrary.load() ;36 37 //給gv控制項加一個監聽器38 //OnGesturePerformedListener監聽器監聽一種手勢(一筆畫完)39 gv.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener(){40 41 @Override42 public void onGesturePerformed(GestureOverlayView overlay,43 Gesture gesture) {44 45 //如果手勢庫載入成功46 if(loadStatus){47 //識別手勢 Prediction是一個相似性對象,集合中的相似性是從高到低進行排列48 ArrayList<Prediction> pres = gestureLibrary.recognize(gesture) ;49 if(!pres.isEmpty()){50 //拿到相似性最高的對象51 Prediction pre = pres.get(0) ;52 //用整型的數表示百分比 >60%53 if(pre.score > 6){54 //拿到手勢的名字判斷進行下一步邏輯55 if("94".equals(pre.name)){56 //說明想關掉當前的activity57 finish() ;58 }else if("yes".equals(pre.name)){59 //說明想打電話了60 Intent intent = new Intent() ;61 intent.setAction(Intent.ACTION_CALL) ;62 intent.setData(Uri.parse("tel://110")) ;63 startActivity(intent) ;64 }else if("666".equals(pre.name)){65 //說明你想彈一個土司66 Toast.makeText(MainActivity.this, "哈哈,我彈出來了", 0).show() ;67 }68 }else{69 Toast.makeText(MainActivity.this, "手勢不匹配", 0).show() ;70 }71 }else{72 Toast.makeText(MainActivity.this, "手勢庫載入失敗", 0).show() ;73 }74 }75 }76 }) ;77
這裡用到了撥打到電話的介面,一定要添加許可權,如所示
這裡之後代碼就玩了,可以進行測試。
我輸入一個6
然後就彈出來了。表示驗證成功。
Android Gesture 手勢建立以及使用樣本