0

我正在尝试使用 GestureBuilder 向应用程序添加一些手势支持。我已经阅读了一些教程并认为我的代码是正确的,但该应用程序仍然没有响应通过 GestureBuilder 创建和导入的手势。它确实响应了我也设置的双击手势。

我认为错误可能是设置的GestureOverlayView所以这里是我的视图的代码,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/scrollviewtexture" >



        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="53dp"
            android:text="@string/app_name"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="9pt"
            android:typeface="sans" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:layout_centerHorizontal="true"
            android:text="@string/swipe"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#FFFFFF"
            android:textSize="6pt" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:height="40dp"
            android:text="@string/app_name"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="7pt" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/textView4"
            android:layout_centerHorizontal="true"
            android:height="40dp"
            android:text="@string/type"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="8pt" />

  <android.gesture.GestureOverlayView
    android:id="@+id/gestures"
    android:layout_width="fill_parent"
    android:layout_height="0dip" />

    </RelativeLayout>

</LinearLayout>

主要活动代码如下,

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
import com.mystraldesign.memorable.PassGen;
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.util.Log;

public class MemorableActivity extends Activity implements android.view.GestureDetector.OnGestureListener,OnDoubleTapListener,OnGesturePerformedListener  
{
    //Define text views
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;

    private GestureLibrary mLibrary;


    //Previous password holder
    private String prevPass;

    //Gesture Detectors
    private GestureDetector gTap; 

    /** Called when the activity is first created. */
    @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);

        mLibrary = GestureLibraries.fromRawResource(this, R.raw.gest);
        if (!mLibrary.load()){
            finish();
        }

        GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
        gestures.addOnGesturePerformedListener(this);

        //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);


        //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 = mLibrary.recognize(gesture);

    if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
        String result = predictions.get(0).name;

        if ("left".equalsIgnoreCase(result)){
            Context context = getApplicationContext();
            CharSequence text = "left";
            int duration = Toast.LENGTH_SHORT;

            //Display message
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

        }else if ("right".equalsIgnoreCase(result)){
            Context context = getApplicationContext();
            CharSequence text = "right";
            int duration = Toast.LENGTH_SHORT;

            //Display message
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

        }else if ("up".equalsIgnoreCase(result)){
            Context context = getApplicationContext();
            CharSequence text = "up";
            int duration = Toast.LENGTH_SHORT;

            //Display message
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

        }else if ("down".equalsIgnoreCase(result)){
            Context context = getApplicationContext();
            CharSequence text = "down";
            int duration = Toast.LENGTH_SHORT;

            //Display message
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

        }
    }

}













    public boolean onTouchEvent(MotionEvent me)
    { 
        this.gTap.onTouchEvent(me);
       return super.onTouchEvent(me); 
    }



    public boolean onDown(MotionEvent e) {

      return false;
    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
          float velocityY) 
    {





    return false;
  }


    public void onLongPress(MotionEvent e) 
    {

    }


    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
      float distanceY) 
    {

        return false;
    }


    public void onShowPress(MotionEvent e) {

    }

    public boolean onSingleTapUp(MotionEvent e) 
    {



     return false;
    }


    //Method to copy password - Depreciated
    public boolean onDoubleTap(MotionEvent e) {

     return false;
    }

    //Method to copy password
    public boolean onDoubleTapEvent(MotionEvent e) {


        //clipboard shite
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
        clipboard.setText(textView1.getText());

        //Message about coping
        Context context = getApplicationContext();
        CharSequence text = "Password has been copied to clipboard.";
        int duration = Toast.LENGTH_SHORT;

        //Display message
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();






      return false;
    }



    public boolean onSingleTapConfirmed(MotionEvent e) {

     return false;
    }



}
4

2 回答 2

0

像这样在您的代码中进行一些编辑。将您
更改gestures.addOnGesturePerformedListener(this);gestures.addOnGesturePerformedListener(handleGestureListener);
然后编写其余代码,例如..

private OnGesturePerformedListener handleGestureListener = new OnGesturePerformedListener() {
    @Override
    public void onGesturePerformed(GestureOverlayView gestureView,Gesture gesture) 
    {
      .
      .
      .
      .
    }
于 2013-01-09T06:16:44.800 回答
0

答案在于视图的布局。GestureOverlay 应该是具有RelativeView 的主人,然后是TextBox 的主人。线性视图被删除。

于 2013-01-10T07:00:32.037 回答