【问题标题】:Android Gesture Recognition badAndroid手势识别不好
【发布时间】:2014-01-20 18:50:40
【问题描述】:

我在 Android 中开发了一个带有手势的应用。 它只是有四种不同的手势。上、下、左、右。 left 和 right 效果很好,但 up 和 down 永远无法识别。它也直接变得不确定(我将颜色设置为红色,以便我可以更快地调试)。

这是我的控制器代码:

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 只是为了看看它是否有效,是的,它有效。

有什么想法吗?谢谢!

【问题讨论】:

    标签: android gesture


    【解决方案1】:

    你有没有调用这个方法:addOnGesturePerformedListener

    我运行了旧版演示,然后我发现OnGesturePerformedListener 导致无法识别单个向上和向下手势。

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多