2011-06-17 36 views
1

+ - × /Android的手勢,單筆劃多衝程產生不一致的結果

我有一個很奇怪的問題,而以下的手勢教程這裏:http://developer.android.com/resources/articles/gestures.html

4個獨特手勢在手勢生成器中創建:+ - ×/

添加和乘法是多衝程。減法和除法是單行程。

活動加載預構建的GestureLibrary(R.raw.gestures),將OnGesturePerformedListener添加到GestureOverlayView,並在檢測到手勢&時執行onGesturePerformed()。在XML)

活動佈局

<android.gesture.GestureOverlayView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gestures" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

android:gestureStrokeType="multiple" 
android:eventsInterceptionEnabled="true" 
android:orientation="vertical" 
/> 

位於的onCreate()

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

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

位於onGesturePerformed(

ArrayList<Prediction> predictions = mLibrary.recognize(gesture); 

    // We want at least one prediction 
    if (predictions.size() > 0) { 
     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(); 
     } 
    } 

主要問題是預構建的手勢未被正確識別。例如,如果水平手勢後跟垂直(加法),則onGesturePerformed()從不執行。方法如果垂直手勢後跟水平,則調用該方法。這也是GesturesListDemo的行爲方式(GesturesDemos.zip @ code.google.com)。

此外,預測手勢最終成爲不正確的手勢。添加被識別爲相減;乘以分水嶺;作爲添加減去。這完全不一致。

最後,添加和減去手勢通常會共享類似的Prediction.score,這是奇怪的,因爲它們相差整個筆畫。

很抱歉,很長一段時間 - 想徹底。任何意見將不勝感激,謝謝大家。

回答

1

我意識到這是一個非常古老的問題,但它還沒有得到回答,這可能有助於某人。我剛剛遇到類似的問題,我的解決方案是使用gesture.getStrokesCount()來區分單筆和多筆筆勢。

例子:

ArrayList<Prediction> predictions = mLibrary.recognize(gesture); 

// We want at least one prediction 
if (predictions.size() > 0) { 
    Prediction prediction = predictions.get(0); 

    // We want at least some confidence in the result 
    if (prediction.score > 1.0) { 
     if (gesture.getStrokesCount() > 1) { 
      // This is either add or multiply 
     } 
     else { 
      // This is either subtract or divide 
     } 
    } 
} 
+0

這是一個很好的解決方案。我在答案中增加了進一步的實現。 – Robbe 2014-11-25 23:46:54

1

大廈德魯萊德曼他的回答,您可以使用此實現onGesturePerformed的始終獲得最佳效果:

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { 

     ArrayList<Prediction> predictions = store.recognize(gesture); 
     double highScore = 0; 
     String gestureName = ""; 

     for (Prediction prediction : predictions) { 

      if (prediction.score > SCORE && prediction.score > highScore && 
        store.getGestures(prediction.name).get(0).getStrokesCount() == gesture.getStrokesCount()) { 

       highScore = prediction.score; 
       gestureName = prediction.name; 

      } 
     } 
     // Do something with gestureName 
    } 
+1

我知道這是舊的,但我只是試着用這個東西。預測列表似乎是排序的,所以你最好在發現一個之後突破,因爲沒有預測會更好。 – PMunch 2015-02-20 21:14:24

+0

@PMunch我不知道這一點。這意味着我們也不需要'highScore'。 – Robbe 2015-02-21 20:03:15

+0

這是我的觀點。我通過減少每次擊中的計數器來實現前三件事,這使得它很容易被預先分類。 – PMunch 2015-02-21 23:28:44