如何实现双击图像视图onclick?

时间:2011-07-16 06:59:11

标签: android

我有一个imageview。我需要在双击和单击时以不同的速度开始动画。双击速度快,单拍速度恢复正常速度。

如何在imageview的onclick上实现手势?

4 个答案:

答案 0 :(得分:10)

如果您不想使用自定义图像视图,则可以使用以下方法

// class level

GestureDetector gestureDetector;
boolean tapped;
ImageView imageView;

// inside onCreate of Activity or Fragment
gestureDetector = new GestureDetector(getActivity(),new GestureListener());

// --------------------------------------------- -----------------------------------

public class GestureListener extends
            GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {

            return true;
        }

        // event when double tap occurs
        @Override
        public boolean onDoubleTap(MotionEvent e) {

            tapped = !tapped;

            if (tapped) {



            } else {



            }

            return true;
        }
    }

// --------------------------------------------- -----------------------------------

for ImageView

imageView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return gestureDetector.onTouchEvent(event);
            }

        });

答案 1 :(得分:5)

您可以实现一个手势监听器来捕捉双击,例如:

public class MyView extends ImageView {

    GestureDetector gestureDetector;

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // creating new gesture detector
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    // skipping measure calculation and drawing

    // delegate the event to the gesture detector
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        return gestureDetector.onTouchEvent(e);
    }


    private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        // event when double tap occurs
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            float x = e.getX();
            float y = e.getY();

            Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")");

            return true;
        }
    }
}

答案 2 :(得分:2)

无需使用gesturelistener,创建自己的自定义imageview,然后覆盖ontouchEvent,如下所示:

long t1 = 0, t2 = 0;
int count = 0;
@Override
public boolean onTouchEvent(MotionEvent e) {

    int action = e.getAction();
    switch (action){
        case MotionEvent.ACTION_DOWN:
            if(count == 0) {
                t1 = System.currentTimeMillis();
            }
            if(count == 1){
                t2 = System.currentTimeMillis();
            }
            count++;
            if(count > 1) count = 0;
            Log.d("DoubleTapImageView", "down t1: " + String.valueOf(t1) + " t2: " + String.valueOf(t2) + "," + String.valueOf(t2-t1));
            if(Math.abs(t2 - t1) < 300){
                t1 = t2 = 0; count = 0;
                // On double tap here. Do stuff
            }

            break;

    }
    return super.onTouchEvent(e);
}

通过这种方式,您可以控制持续时间

答案 3 :(得分:0)

您需要在活动上扩展GestureDetector.SimpleOnGestureListener。然后你将获得onDoubleTap事件。有关详细信息,您可以在此处引用相同的帖子:

DoubleTap in android

Android: How to detect double-tap?