覆盖libs onTouch侦听器

时间:2015-06-09 22:42:33

标签: android android-imageview

我正在使用PhotoView https://github.com/chrisbanes/PhotoView
它自己已经在你提供的OnTouchListener上实现了ImageView

这是我的问题,我需要实现OnLongTouchListener并且在其中我需要X & Y coordinates触摸事件。问题是我无法从OnLongTouchListener->onLongClick(View ..)获取这些内容,这意味着我还需要实现自己的OnTouchListenerX & Y获取MotionEvent将它们置于类变量中而不是我的事物中OnLongTouchListener

然而,我自己的OnTouchListener覆盖了PhotoViews监听器,删除了缩放和放大器。滑动功能。

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import java.util.Observable;

import uk.co.senab.photoview.PhotoViewAttacher;

public class MapView extends Observable {

    private MainActivity activity;
    private PhotoViewAttacher photoView;
    private Matrix viewMatrix;

    protected ImageView view;
    protected Bitmap bitmap;

    public MapView(MainActivity activity) {
        this.activity = activity;

        view = (ImageView) activity.findViewById(R.id.map_view);
        bitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.floorplan);

        photoView = new PhotoViewAttacher(view);
        viewMatrix = view.getImageMatrix();

        view.setOnTouchListener(new MapViewTouchListener());
        view.setOnLongClickListener(new MapViewLongTouchListener());
    }

    private class MapViewLongTouchListener implements View.OnLongClickListener {

        public boolean onLongClick(View v) {
            Log.i("output", "Long press");
            return true;
        }

    }

    private class MapViewTouchListener implements View.OnTouchListener {

        public boolean onTouch(View v, MotionEvent event) {
            Log.i("output", "Touch");
            return true;
        }

    }
}

临时解决方案。我创建了自己的类并覆盖了onTouch方法。原来PhotoViewAttacher实现OnTouchListener所以我能够让它重新实现该方法。

3 个答案:

答案 0 :(得分:0)

要检查长按,我想你想要这样的东西:

if(event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN && event.getActionIndex()==0){
    mFingerTimeDown = System.currentTimeMillis();
}

if(event.getActionMasked() == MotionEvent.ACTION_POINTER_UP &&     event.getActionIndex()==0 ){
    if ((System.currentTimeMillis()-mFingerDownTime) >= LONG_PRESS_TIME_MILLIS)
        //long press action
    else
        //short press action
}

答案 1 :(得分:0)

尝试在侦听器中返回false。如果listener事件方法返回true,则表示事件已处理,并且不会传播到下面的视图。

答案 2 :(得分:0)

解决方案是简单地扩展PhotoViewAttacher类工具OnLongClickListener,并从内部MotionEvent方法传递onTouch

private class ZoomableMapView extends PhotoViewAttacher implements OnLongClickListener {

  private MotionEvent event;

  public ZoomableMapView(ImageView view) {
    super(view);
  }

  public boolean onTouch(View view, MotionEvent event) {
    this.event = event;
    return super.onTouch(view, event):
  } 

  public boolean onLongClickListener(View view) {
    // Access event.getX.getY... here
    ...
    return false;
  }
}