Android手势无法识别

时间:2013-01-10 06:52:28

标签: android gesture gesture-recognition gestures

我正在尝试使用手势构建器在我的应用上接收上/下和左/右手势。我按照这里的指南(http://www.vogella.com/articles/AndroidGestures/article.html),它似乎工作,但没有完全响应手势正确。它在显示屏上左右显示为亮黄色(找到手势),但当Toast创建弹出窗口以显示手势的名称时,它会显示右/上/下全部被识别或左/上/下来了。此外,它无法将实际滑动上下识别为有效手势(叠加时淡出黄色)。

我尝试过多次使用Gesture Builder重建Gesture库,以防出现错误,但似乎没有。

正在使用的代码与上面的链接相同。

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    gTap  = new GestureDetector(this,(android.view.GestureDetector.OnGestureListener) this);

    //Remove title bar
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);


    //Define textView
    textView1 = (TextView) findViewById(R.id.textView1);
    textView2 = (TextView) findViewById(R.id.textView2);
    textView3 = (TextView) findViewById(R.id.textView3);
    textView4 = (TextView) findViewById(R.id.textView4);

    GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
    View inflate = getLayoutInflater().inflate(R.layout.main, null);
    gestureOverlayView.addView(inflate);
    gestureOverlayView.addOnGesturePerformedListener(this);

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


    //Load font file
    Typeface type = Typeface.createFromAsset(getAssets(),"fonts/optima.ttf"); 

    //Set various textViews to font
    textView1.setTypeface(type);
    textView2.setTypeface(type);
    textView3.setTypeface(type);
    textView4.setTypeface(type);

    prevPass = "Memorable";

}


public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
  ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
  for (Prediction prediction : predictions) {
    if (prediction.score > 1.0) {
      Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT)
          .show();
    }
  }
}

1 个答案:

答案 0 :(得分:1)

如上所述here,请将此添加到您的代码中。

GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
    gestures.setGestureStrokeAngleThreshold( 90.0f);

Here是Gesture Builder应用程序的完整代码。你也可以尝试一下。