2013-01-09 59 views
0

我正在嘗試使用GestureBuilder嚮應用添加手勢支持。我已閱讀了一些教程,並認爲我的代碼是正確的,但該應用程序仍不會響應通過GestureBuilder創建和導入的手勢。它確實響應我也設置的doubleTap手勢。Android手勢生成器不工作

我認爲錯誤可能是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; 
    } 



} 

回答

0

做一些在你的代碼編輯像這樣..
更改您的gestures.addOnGesturePerformedListener(this);gestures.addOnGesturePerformedListener(handleGestureListener);
然後編寫代碼的其餘部分,如..

private OnGesturePerformedListener handleGestureListener = new OnGesturePerformedListener() { 
    @Override 
    public void onGesturePerformed(GestureOverlayView gestureView,Gesture gesture) 
    { 
     . 
     . 
     . 
     . 
    } 
0

答案躺在視圖的佈局中。 GestureOverlay應該是用RelativeView和TextBox裏面的主人。線性視圖被刪除。