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

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

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

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

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

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

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

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

下面,我們就開(kāi)始寫(xiě)布局文件

<?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> 

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

public class MainActivity extends Activity { 
 private boolean success; 
 // 定義手勢(shì)庫(kù) 
 private GestureLibrary library; 
 private GestureOverlayView gestureView; 
 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
  // 找到手勢(shì)庫(kù) 
  library = GestureLibraries.fromRawResource(this, R.raw.gestures); 
  // 加載手勢(shì)庫(kù) 
  success = library.load(); 
  gestureView = (GestureOverlayView) this.findViewById(R.id.gestures); 
  // 添加事件監(jiān)聽(tīng)器 
  gestureView.addOnGesturePerformedListener(new GestureListener()); 
 } 
 
 private final class GestureListener implements OnGesturePerformedListener { 
  @Override 
  public void onGesturePerformed(GestureOverlayView overlay, 
    Gesture gesture) { 
   // 如果手勢(shì)庫(kù)加載成功 
   if (success) { 
    // 從手勢(shì)庫(kù)中查找匹配的手勢(shì),最匹配的記錄會(huì)放在最前面 
    ArrayList<Prediction> predictions = library.recognize(gesture); 
    if (!predictions.isEmpty()) { 
     // 獲取第一個(gè)匹配的手勢(shì) 
     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()); 
      // 撥打電話(huà) 
      } else if ("5556".equals(prediction.name)) { 
       Intent intent = new Intent(Intent.ACTION_CALL, 
         Uri.parse("tel:5556")); 
       startActivity(intent); 
      } 
     } 
    } 
   } 
  } 
 
 } 
} 

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

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

相關(guān)文章

最新評(píng)論