GestureDetector不适用于我的View

时间:2012-06-16 17:02:16

标签: java android

我一直在开发绘图应用程序,我需要通过视图检测双击:

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中看到任何笔记。请告诉我为什么。

2 个答案:

答案 0 :(得分: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);
        }
    });

答案 1 :(得分:-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);
    }
});