Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement touch listener on image?

Tags:

android

I am developing an application. In my application I am display images using ImageView from url using xml parsing. I want to display zoom image when I double touch on the Image, then again double touch on zoomImage, I want to reset image. How to implement in image using Android?

like image 762
JohnNick Avatar asked Nov 17 '10 17:11

JohnNick


People also ask

How to implement on touch listener in android?

setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { mDetector. onTouchEvent(event); return true; }}); I would override the methods in MyGestureDetector and log to logcat like I'm doing on double tap to get a feel for how this works.

How do I use onTouchEvent on Android?

After the Math. abs() calls, you're essentially testing if their finger is farther down the screen than it is across the screen. Store the initial down coordinates as member variables and set them during ACTION_DOWN . You declared two floats (touchX and touchY) inside the onTouchEvent method.


1 Answers

This is how you implement a touch listener in Android.

        yourImageView.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                return false;
            }
       });

To detect double taps you use GestureDetector like this:

1) Make your own GestureDetector, derived from SimpleOnGestureListener and override the methods you're interested in (see google's docs on SimpleOnGestureListener for the exact methods you can override, I've done double tap here):

class MyGestureDetector extends GestureDetector.SimpleOnGestureListener
{

@Override
 public boolean onDoubleTapEvent(MotionEvent e)
 {
    Log.i("Taghere..", "onDoubleTapEvent");
    return true;
    }
}

2) Create an instance of your gesture detector. I'm making a member variable and instantiating in onCreate.

    private GestureDetector mDetector;

    mDetector = new GestureDetector(this, new MyGestureDetector());

3) Setup a touch listener on your imageview and route the messages to your gesture detector:

ImageView iv = (ImageView)findViewById(R.id.yourimageviewid);
iv.setOnTouchListener(new OnTouchListener(){

@Override
public boolean onTouch(View v, MotionEvent event)
{
    mDetector.onTouchEvent(event);
    return true;
}});

I would override the methods in MyGestureDetector and log to logcat like I'm doing on double tap to get a feel for how this works.

like image 67
Ryan Reeves Avatar answered Oct 04 '22 08:10

Ryan Reeves