Android實現Gesture手勢識別用法分析_Android

來源:互聯網
上載者:User

本文執行個體分析了Android實現Gesture手勢識別用法。分享給大家供大家參考。具體如下:

很高興能在Android1.6的sdk看到手勢識別這一功能,之前一直在想,如何在android中實現nds遊戲那樣用手勢(準確點應該是筆勢)來控制遊戲角色?現在總算看到一點曙光了,不過手勢要做到筆勢那樣隨心所欲地控制遊戲人物,還有很多細節問題需要處理。

在Android1.6的模擬器裡面預裝了一個叫Gestures Builder的程式,這個程式就是讓你建立自己的手勢的(Gestures Builder的原始碼在sdk問samples裡面有,有興趣可以看看)。建立的手勢將被儲存到/sdcard/gestures裡面,把這個檔案複製到你的工程/res/raw下,你就可以在你的工程裡面使用這些手勢了。複製到/res/raw下的手勢是唯讀,也就是說你不能修改或增加手勢了,如果想實現增改的話,可以直接載入sd卡裡面的gestures檔案。

在例子中,我建立了這樣的手勢:

第二步:在layout裡面建立GestureOverlayView,這個透明的view就是讓你在上面畫手勢用的,可以疊在其他View上面:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  ><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="@string/hello"  /><android.gesture.GestureOverlayView  android:id="@+id/gestures"  android:layout_width="fill_parent"  android:layout_height="0dip"  android:layout_weight="1.0"  /></LinearLayout>

第三步:載入Gesture:

mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);if (!mLibrary.load()) {  finish();}

第四步:增加響應函數OnGesturePerformedListener:

GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);gestures.addOnGesturePerformedListener(this);

以上四步就可以實現簡單的Gesture識別原型了:

程式運行結果如下,書寫一個a字,程式識別出,然後toast一個a出來:

完整代碼如下:

package com.ray.test;import java.util.ArrayList;import android.app.Activity;import android.gesture.Gesture;import android.gesture.GestureLibraries;import android.gesture.GestureLibrary;import android.gesture.GestureOverlayView;import android.gesture.Prediction;import android.gesture.GestureOverlayView.OnGesturePerformedListener;import android.os.Bundle;import android.widget.Toast;public class TestGesture extends Activity implements OnGesturePerformedListener{  GestureLibrary mLibrary;  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);    gestures.addOnGesturePerformedListener(this);    mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);    if (!mLibrary.load()) {      finish();    }  }  @Override  public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {    ArrayList predictions = mLibrary.recognize(gesture);    // We want at least one prediction    if (predictions.size() > 0) {      Prediction prediction = (Prediction) predictions.get(0);      // We want at least some confidence in the result      if (prediction.score > 1.0) {        // Show the spell        Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();      }    }  }}

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android編程之activity操作技巧總結》、《Android視圖View技巧總結》、《Android操作SQLite資料庫技巧總結》、《Android操作json格式資料技巧總結》、《Android資料庫操作技巧總結》、《Android檔案操作技巧匯總》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》、《Android資源操作技巧匯總》及《Android控制項用法總結》

希望本文所述對大家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.