5

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
rajvi
  • 51
  • 1
  • 1
  • 2

4 Answers4

12

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

        });
Nayanesh Gupte
  • 2,735
  • 25
  • 37
5

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;
        }
    }
}
jobesu
  • 620
  • 6
  • 7
2

No need to use gesturelistener, create your own custom imageview then override the ontouchEvent like this:

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

By this way, you can control the duration time

0

You need to extend GestureDetector.SimpleOnGestureListener on your activity. Then you will get onDoubleTap event. For more details you can refer same posts over here:

DoubleTap in android

Android: How to detect double-tap?

Community
  • 1
  • 1
Balaji Khadake
  • 3,449
  • 4
  • 24
  • 38