Reputation: 43
I've developed an app with gestures in Android. It simply have four different gestures. Up, down, left and right. Left and right works well, but up and down is never recognized. It also directly gets uncertain (I set the color to red so I can debug a faster way).
Here's my controller code:
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();
}
}
}
}
And my view:
<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>
R is just to see if it works and yes, it does.
Any ideas? Thank you!
Upvotes: 1
Views: 238
Reputation: 123
Have u called this method : addOnGesturePerformedListener
?
I runned the legacy demo then I found that OnGesturePerformedListener
causes single up&down gesture not be recognized.
Upvotes: 0