Android Gesture 手勢創建以及使用示例

jopen 9年前發布 | 22K 次閱讀 Android Android開發 移動開發

    在Android1.6的模擬器里面預裝了一個叫Gestures Builder的程序,這個程序就是讓你創建自己的手勢的(Gestures Builder的源代碼在sdk問samples里面有,有興趣可以看看)</span>

 

將上面這四個文件復制到你的工程目錄下面,如圖所示

在模擬器上面運行這個工程文件,在模擬器上面創建一些手勢文件,例如:

 

創建的手勢將被保存到/mnt/sdcard/gestures里面,然后新建一個測試的手勢項目文件,將gestures文件復制到res目錄中的raw文件下面,

然后配置xml文件,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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.xunfang.gesture.MainActivity" >

    <android.gesture.GestureOverlayView 
        android:id="@+id/gv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:gestureStrokeWidth="10"
        android:gestureColor="#ff0000"
        />


</RelativeLayout>

GestureOverlayView:一種用于手勢輸入的透明覆蓋層,可覆蓋在其他控件的上方,也可包含其他控件。
Android:gestureStrokeType 定義筆畫(定義為手勢)的類型
Android:gestureStrokeWidth 畫手勢時,筆劃的寬度

activity文件內容如下
package com.xunfang.gesture;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends Activity {

    private GestureOverlayView gv ;

    private boolean loadStatus ;

    private GestureLibrary gestureLibrary ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //拿到控件
        gv = (GestureOverlayView) findViewById(R.id.gv) ;

        //創建加載手勢庫的工具
        gestureLibrary =  GestureLibraries.fromRawResource(this, R.raw.gestures) ;
        //加載手勢庫
        loadStatus = gestureLibrary.load() ;

        //給gv控件加一個監聽器
        //OnGesturePerformedListener監聽器監聽一種手勢(一筆畫完)
        gv.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener(){

            @Override
            public void onGesturePerformed(GestureOverlayView overlay,
                    Gesture gesture) {

                //如果手勢庫加載成功
                if(loadStatus){
                    //識別手勢  Prediction是一個相似度對象,集合中的相似度是從高到低進行排列
                     ArrayList<Prediction> pres = gestureLibrary.recognize(gesture) ;
                     if(!pres.isEmpty()){
                         //拿到相似度最高的對象
                         Prediction pre = pres.get(0) ;
                         //用整型的數表示百分比  >60%
                         if(pre.score > 6){
                             //拿到手勢的名字判斷進行下一步邏輯
                             if("94".equals(pre.name)){
                                 //說明想關掉當前的activity
                                 finish() ;
                             }else if("yes".equals(pre.name)){
                                 //說明想打電話了
                                 Intent intent = new Intent() ;
                                 intent.setAction(Intent.ACTION_CALL) ;
                                 intent.setData(Uri.parse("tel://110")) ;
                                 startActivity(intent) ;
                             }else if("666".equals(pre.name)){
                                 //說明你想彈一個土司
                                 Toast.makeText(MainActivity.this, "哈哈,我彈出來了", 0).show() ;
                             }
                         }else{
                             Toast.makeText(MainActivity.this, "手勢不匹配", 0).show() ;
                         }
                     }else{
                         Toast.makeText(MainActivity.this, "手勢庫加載失敗", 0).show() ;
                     }
                }
            }
        }) ;

這里用到了撥打電話的界面,一定要添加權限,如下圖所示

 

這里之后代碼就玩了,可以進行測試。

我輸入一個6

然后就彈出來了。表示驗證成功。

來自:http://www.cnblogs.com/ouysq/p/4639496.html

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!