2014-01-20 171 views
1

我已经开发了一个在Android中使用手势的应用程序。 它只是有四种不同的手势。向上,向下,向左和向右。 左右方向运作良好,但上下方向永远不会被识别。它也直接变得不确定(我将颜色设置为红色,这样我可以更快地调试)。Android手势识别不好

这里是我的控制器代码:

import java.util.ArrayList; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.gesture.Gesture; 
import android.gesture.GestureLibraries; 
import android.gesture.GestureLibrary; 
import android.gesture.GestureOverlayView; 
import android.gesture.GestureOverlayView.OnGesturePerformedListener; 
import android.gesture.Prediction; 
import android.graphics.Color; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.Window; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 


public class PreperationActivity extends Activity implements OnGesturePerformedListener { 

    private GestureLibrary gestureLib; 
    private ImageView ivProfileImage; 
    private TextView list1, list2, list3, list4; 
    private TextView tvUsername; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

     GestureOverlayView gestureOverlayView = new GestureOverlayView(this); 
     View inflate = getLayoutInflater().inflate(R.layout.activity_preperation, null); 
     gestureOverlayView.addView(inflate); 
//  gestureOverlayView.setGestureVisible(false); 
     gestureOverlayView.setGestureColor(Color.BLACK); 
     gestureOverlayView.setUncertainGestureColor(Color.RED); 
     gestureOverlayView.addOnGesturePerformedListener(this); 
     gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures); 
     if (!gestureLib.load()) { 
      finish(); 
     } 

     setContentView(gestureOverlayView); 
     ivProfileImage = (ImageView) findViewById(R.id.ivProfileImage); 
     ivProfileImage.setImageBitmap(ListActivity.friends.peek().profileImage); 

     Intent intent = getIntent(); 

     list1 = (TextView) findViewById(R.id.list1); 
     list2 = (TextView) findViewById(R.id.list2); 
     list3 = (TextView) findViewById(R.id.list3); 
     list4 = (TextView) findViewById(R.id.list4); 

     list1.setText(intent.getStringExtra("list1")); 
     list2.setText(intent.getStringExtra("list2")); 
     list3.setText(intent.getStringExtra("list3")); 
     list4.setText(intent.getStringExtra("list4")); 

     tvUsername = (TextView) findViewById(R.id.tvUsername); 
     tvUsername.setText(ListActivity.friends.peek().username); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.preperation, menu); 
     return true; 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 
    } 

    @Override 
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { 

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

     for (Prediction prediction : predictions) { 
      Log.i("ced", "PREDICTION: " + prediction.score); 
      if (prediction.score > 1.0) { 
       Friend friend =ListActivity.friends.pop(); 
       ivProfileImage.setImageBitmap(friend.profileImage); 
       tvUsername.setText(friend.username); 
       Toast.makeText(getBaseContext(), prediction.name, Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 
} 

而我的观点:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@color/background" 
tools:context=".PreperationActivity" > 

<TextView 
    android:id="@+id/list1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="top" 
    android:background="@color/list_area_background" 
    android:gravity="center" 
    android:text="" 
    android:textAlignment="center" /> 

<TextView 
    android:id="@+id/list2" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_gravity="right" 
    android:background="@color/list_area_background" 
    android:gravity="center" 
    android:text="" 
    android:textAlignment="center" /> 

<TextView 
    android:id="@+id/list3" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:background="@color/list_area_background" 
    android:gravity="center" 
    android:text="" 
    android:textAlignment="center" /> 

<TextView 
    android:id="@+id/list4" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_gravity="left" 
    android:background="@color/list_area_background" 
    android:gravity="center" 
    android:text="" 
    android:textAlignment="center" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <ImageView 
    android:id="@+id/ivProfileImage" 
    android:layout_width="183dp" 
    android:layout_height="183dp" 
    android:src="@drawable/com_facebook_profile_picture_blank_square" /> 

    <TextView 
    android:id="@+id/tvUsername" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textColor="#ffffff" 
    android:layout_marginTop="5pt" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
</LinearLayout> 

My gestures from the Gesture Builder

R是只是为了看看它的工作原理,是的,是的。

任何想法?谢谢!

回答

0

你有没有叫这个方法:addOnGesturePerformedListener

我跑了遗留演示,然后我发现OnGesturePerformedListener导致单身&下来的手势不被识别。