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>
R是隻是爲了看看它的工作原理,是的,是的。
任何想法?謝謝!