2011-07-31 281 views
0

我導入了手勢示例並創建了自己的應用程序。佈局中有一個按鈕和gestureoverlayview。該按鈕啓動GestureBuilderActivity.class,我可以在其中添加或刪除手勢(這是示例)。在按鈕下方,在GestureOverlayView中,我可以繪製手勢。佈局:Android識別手勢

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Click to see gestures" 
    android:id="@+id/Button01" 
    /> 
    <android.gesture.GestureOverlayView 
    android:id="@+id/gestures" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1.0" /> 
</LinearLayout> 

從例子中,我知道,這就是我的手勢:

final String path = new File(Environment.getExternalStorageDirectory(), 
        "gestures").getAbsolutePath(); 

和吐司味精節目(仍處於例子)手勢被保存在/ mnt/SD卡/手勢:

Toast.makeText(this, getString(R.string.save_success, path), Toast.LENGTH_LONG).show(); 

我該如何讓應用程序識別我畫的手勢並在烤麪包中顯示我的名字?

+0

在這個問題http://stackoverflow.com/questions/18165847/android-multi-stroke-gesture-not-working你有單筆畫手勢識別的工作代碼。這就是所有你需要做的。至於多筆劃符號。我自己也在努力。 – LoveMeow

回答

0

首先,我會讀這short article,解釋如何使用手勢。

要識別手勢,您需要將OnGesturePerformedListener提供給GestureOverlayView。在這個例子中,我只是讓Activity實現了OnGesturePerformedListener。

public class MyActivity extends Activity implements OnGesturePerformedListener { 

private GestureLibrary mGestures; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // I use resources but you may want to use GestureLibraries.fromFile(path) 
    mGestures = GestureLibraries.fromRawResource(this, R.raw.gestures); 
    if (!mGestures.load()) { 
     showDialog(DIALOG_LOAD_FAIL); 
     finish(); 
    } 

    // register the OnGesturePerformedListener (i.e. this activity) 
    GestureOverlayView gesturesView = (GestureOverlayView) findViewById(R.id.gestures); 
    gesturesView.addOnGesturePerformedListener(this); 
} 

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { 
    ArrayList<Prediction> predictions = mGestures.recognize(gesture); 

     // Determine if a gesture was performed. 
     // This can be tricky, here is a simple approach. 
    Prediction prediction = (predictions.size() != 0) ? predictions.get(0) : null; 
    prediction = (prediction.score >= 1.0) ? prediction: null; 

    if (prediction != null) { 
      // do something with the prediction 
      Toast.makeText(this, prediction.name + "(" + prediction.score + ")", Toast.LENGTH_LONG).show(); 
    } 
}