开发者

How to implement doubletap on imageview's onclick?

开发者 https://www.devze.com 2023-03-20 13:38 出处:网络
I have an imageview. I need to start animation with different speed on double tap and singletap. On doubletap speed is fast and on 开发者_如何学JAVAsingletap speed is back to normal speed.

I have an imageview. I need to start animation with different speed on double tap and singletap. On doubletap speed is fast and on 开发者_如何学JAVAsingletap speed is back to normal speed.

How can I implement gestures on imageview's onclick?


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

        });


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


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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消