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

Android使用GestureOverlayView控件實(shí)現(xiàn)手勢識別

 更新時間:2018年04月18日 16:57:15   作者:趙凱強(qiáng)  
這篇文章主要為大家詳細(xì)介紹了Android使用GestureOverlayView控件實(shí)現(xiàn)手勢識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在Android開發(fā)中,我們不光可以使用已有的實(shí)現(xiàn)方式,而且,我們還可以利用Android這個智能手機(jī)平臺,實(shí)現(xiàn)一些比較有特色的功能。本篇文章介紹使用GestureOverlayView這個控件,實(shí)現(xiàn)簡單的手勢識別的小例子。

首先,在使用手勢識別之前,我們需要建立一個手勢庫,創(chuàng)建手勢庫,我們可以找到sdk自帶的實(shí)例程序,比如我本地的路徑為sdk\samples\android-18\input\gestures,找到這個程序,然后建立一個新項(xiàng)目,將其整合之后,就可以用于產(chǎn)生手勢庫。

整合之后的項(xiàng)目結(jié)構(gòu)如下

下面是運(yùn)行界面

產(chǎn)生的手勢庫,默認(rèn)存放在sd卡的根目錄下面,我們將生成的手勢庫文件放在我們的raw目錄下面

下面,我們就開始寫布局文件

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

布局很簡單,就是一個用于接受手勢的控件,下面是邏輯代碼的實(shí)現(xiàn)

public class MainActivity extends Activity { 
 private boolean success; 
 // 定義手勢庫 
 private GestureLibrary library; 
 private GestureOverlayView gestureView; 
 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
  // 找到手勢庫 
  library = GestureLibraries.fromRawResource(this, R.raw.gestures); 
  // 加載手勢庫 
  success = library.load(); 
  gestureView = (GestureOverlayView) this.findViewById(R.id.gestures); 
  // 添加事件監(jiān)聽器 
  gestureView.addOnGesturePerformedListener(new GestureListener()); 
 } 
 
 private final class GestureListener implements OnGesturePerformedListener { 
  @Override 
  public void onGesturePerformed(GestureOverlayView overlay, 
    Gesture gesture) { 
   // 如果手勢庫加載成功 
   if (success) { 
    // 從手勢庫中查找匹配的手勢,最匹配的記錄會放在最前面 
    ArrayList<Prediction> predictions = library.recognize(gesture); 
    if (!predictions.isEmpty()) { 
     // 獲取第一個匹配的手勢 
     Prediction prediction = predictions.get(0); 
     // 如果匹配度>30%,就執(zhí)行下面的操作 
     if (prediction.score > 3) { 
      // 關(guān)閉應(yīng)用 
      if ("agree".equals(prediction.name)) { 
       android.os.Process.killProcess(android.os.Process 
         .myPid()); 
      // 撥打電話 
      } else if ("5556".equals(prediction.name)) { 
       Intent intent = new Intent(Intent.ACTION_CALL, 
         Uri.parse("tel:5556")); 
       startActivity(intent); 
      } 
     } 
    } 
   } 
  } 
 
 } 
} 

經(jīng)過這幾個步驟,我們就實(shí)現(xiàn)了最簡單的手勢識別的功能了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論