欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)Gesture手勢(shì)識(shí)別用法分析

 更新時(shí)間:2016年09月16日 00:51:44   作者:ztp800201  
這篇文章主要介紹了Android實(shí)現(xiàn)Gesture手勢(shì)識(shí)別用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android基于Gesture實(shí)現(xiàn)手勢(shì)識(shí)別的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例分析了Android實(shí)現(xiàn)Gesture手勢(shì)識(shí)別用法。分享給大家供大家參考。具體如下:

很高興能在Android1.6的sdk看到手勢(shì)識(shí)別這一功能,之前一直在想,如何在android中實(shí)現(xiàn)nds游戲那樣用手勢(shì)(準(zhǔn)確點(diǎn)應(yīng)該是筆勢(shì))來(lái)控制游戲角色?現(xiàn)在總算看到一點(diǎn)曙光了,不過(guò)手勢(shì)要做到筆勢(shì)那樣隨心所欲地控制游戲人物,還有很多細(xì)節(jié)問(wèn)題需要處理。

在Android1.6的模擬器里面預(yù)裝了一個(gè)叫Gestures Builder的程序,這個(gè)程序就是讓你創(chuàng)建自己的手勢(shì)的(Gestures Builder的源代碼在sdk問(wèn)samples里面有,有興趣可以看看)。創(chuàng)建的手勢(shì)將被保存到/sdcard/gestures里面,把這個(gè)文件復(fù)制到你的工程/res/raw下,你就可以在你的工程里面使用這些手勢(shì)了。復(fù)制到/res/raw下的手勢(shì)是只讀的,也就是說(shuō)你不能修改或增加手勢(shì)了,如果想實(shí)現(xiàn)增改的話,可以直接加載sd卡里面的gestures文件。

在例子中,我創(chuàng)建了這樣的手勢(shì):

第二步:在layout里面創(chuàng)建GestureOverlayView,這個(gè)透明的view就是讓你在上面畫手勢(shì)用的,可以疊在其他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();
}

第四步:增加響應(yīng)函數(shù)OnGesturePerformedListener:

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

以上四步就可以實(shí)現(xiàn)簡(jiǎn)單的Gesture識(shí)別原型了:

程序運(yùn)行結(jié)果如下,書寫一個(gè)a字,程序識(shí)別出,然后toast一個(gè)a出來(lái):

完整代碼如下:

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();
      }
    }
  }
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論