Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

android - How to implement touch listener on image?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...