Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement doubletap on imageview's onclick?

Tags:

android

I have an imageview. I need to start animation with different speed on double tap and singletap. On doubletap speed is fast and on singletap speed is back to normal speed.

How can I implement gestures on imageview's onclick?

like image 564
rajvi Avatar asked Jul 16 '11 06:07

rajvi


2 Answers

if you do not wish to go for custom image view then you can use following approach

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

        });
like image 123
Nayanesh Gupte Avatar answered Sep 25 '22 01:09

Nayanesh Gupte


You can implement a gesture listener for catching the double tap for example like that:

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;
        }
    }
}
like image 41
jobesu Avatar answered Sep 25 '22 01:09

jobesu