2012-06-16 60 views
2

我一直在開發繪圖應用程序的工作,我需要通過以檢測雙擊:GestureDetector沒有我的觀點

package com.nda.painter.library.views; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.util.Log; 
import android.view.GestureDetector; 
import android.view.GestureDetector.OnDoubleTapListener; 
import android.view.GestureDetector.OnGestureListener; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnLongClickListener; 
import android.view.View.OnTouchListener; 

import com.nda.painter.library.Painter; 
import com.nda.painter.library.listeners.DrawingListener; 

public class PainterView extends View implements 
    DrawingListener, OnTouchListener, 
    OnGestureListener, OnDoubleTapListener { 

    private GestureDetector detector; 
    private Context context; 
    private Painter painter; 
    private Bitmap bitmap; 
    private Paint bitmapPaint; 
    private Path path; 
    private Paint paint; 

    private float x; 
    private float y; 

    public PainterView(Context context, Painter painter) { 

     super(context); 
     this.context=context; 
     this.painter=painter; 
     //setClickable(true); 
     //setLongClickable(true); 
     //setOnTouchListener(this); 
     detector=new GestureDetector(context, this); 
     detector.setIsLongpressEnabled(true); 
     detector.setOnDoubleTapListener(this); 

    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
    } 

    public void setPainter(Painter painter) { 
     this.painter=painter; 
    } 

    @Override 
    public boolean onDown(MotionEvent e) { 
     // TODO Auto-generated method stub 
     Log.e("event", "event"); 
     return false; 
    } 

    @Override 
    public void onShowPress(MotionEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
      float distanceY) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public void onLongPress(MotionEvent e) { 
     // TODO Auto-generated method stub 
     Log.e("event", "long"); 
     painter.drawString(x,y,"123"); 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean onSingleTapConfirmed(MotionEvent e) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean onDoubleTap(MotionEvent e) { 
     // TODO Auto-generated method stub 
     Log.e("event", "double"); 
     return true; 
    } 

    @Override 
    public boolean onDoubleTapEvent(MotionEvent e) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
} 

請不要在其他代碼中看到的,只是在GestureDetector 。它不起作用!我在Log中沒有看到任何筆記。請告訴我爲什麼。

回答

1

試試這個:

 detector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() { 
     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; // the magic is here 
     } 

     @Override 
     public boolean onDoubleTap(MotionEvent e) { 
      Ln.d("!!! onDoubleTap"); 
      return super.onDoubleTap(e); 
     } 

     @Override 
     public boolean onDoubleTapEvent(MotionEvent e) { 
      Ln.d("!!! onDoubleTapEvent"); 
      return super.onDoubleTapEvent(e); 
     } 
    }); 

    setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      return detector.onTouchEvent(motionEvent); 
     } 
    }); 
+0

謝謝。但雙擊還沒有奏效。 – user1445877

+0

您是否試圖評論此代碼: detector.setIsLongpressEnabled(true); detector.setOnDoubleTapListener(this); 您的手勢檢測器中已有所有觸摸事件,您無需單獨重新定義偵聽器。 –

+0

是的,但它不能幫助我 – user1445877

-1
detector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() { 
    @Override 
    public boolean onDown(MotionEvent e) { 
     return true; 
    } 

    @Override 
    public boolean onDoubleTap(MotionEvent e) { 
     Ln.d("!!! onDoubleTap"); 
     return super.onDoubleTap(e); 
    } 

    @Override 
    public boolean onDoubleTapEvent(MotionEvent e) { 
     Ln.d("!!! onDoubleTapEvent"); 
     return super.onDoubleTapEvent(e); 
    } 
}); 

setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     return detector.onTouchEvent(motionEvent); 
    } 
});