如何在图像上实现触摸监听?

Joh*_*ick 15 android

我正在开发一个应用程序.在我的应用程序中,我ImageView使用xl解析使用url 显示图像.我想在双击图像时显示缩放图像,然后再次双击zoomImage,我想重置图像.如何使用Android在图像中实现?

Rya*_*ves 45

这是您在Android中实现触摸侦听器的方式.

        yourImageView.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                return false;
            }
       });
Run Code Online (Sandbox Code Playgroud)

要检测双击,您可以使用GestureDetector,如下所示:

1)创建自己的GestureDetector,派生自SimpleOnGestureListener并覆盖您感兴趣的方法(请参阅SimpleOnGestureListener上的google文档,了解您可以覆盖的确切方法,我已经完成了双击):

class MyGestureDetector extends GestureDetector.SimpleOnGestureListener
{

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

2)创建手势检测器的实例.我正在创建一个成员变量并在onCreate中实例化.

    private GestureDetector mDetector;

    mDetector = new GestureDetector(this, new MyGestureDetector());
Run Code Online (Sandbox Code Playgroud)

3)在imageview上设置触摸侦听器并将消息路由到手势检测器:

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

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

我会覆盖MyGestureDetector中的方法并记录到logcat,就像我正在双击一样来了解它是如何工作的.