Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override single tap of Photoview

Tags:

java

android

I have an ImageView inside of a view pager with an ActionBar at the top. I would like to be able to single tap to hide the action bar, and I would also like to be able to pinch zoom and pan on each ImageView.

To implement the single tap to hide the action bar I have a simple OnClickListener that hides it.

To implement the pinch zoom and pan on each ImageView I am using the PhotoView Library Project.

I am having issues because only one touch event listener can be associated with an ImageView, and the implementing the PhotoView Library project overwrites my OnClickListener to hide the ActionBar with,

parent.requestDisallowInterceptTouchEvent(true);

I am not sure how to go about getting both implemented at the same time. It seems like the only solution is to create my own Pinch Zoom ImageView in order to control touch events myself.

like image 614
yiati Avatar asked Dec 31 '13 15:12

yiati


Video Answer


2 Answers

Found out that the PhotoView library actually allows me to set onViewTap for the PhotoViewAttacher object which is exactly what I wanted.

To create the PhotoViewAttacher in the current Fragment/Activity have it implement PhotoViewAttacher.OnViewTapListener, create the attacher,

PhotoViewAttacher mAttacher = new PhotoViewAttacher(imageView);
mAttacher.setOnViewTapListener(this);

and add the following function,

public void onViewTap(View view, float x, float y) {
    // your code here
}

Source

like image 184
yiati Avatar answered Oct 22 '22 12:10

yiati


You'll have to override the PhotoView library itself. If you look at the source code, the PhotoViewAttacher class is the one that handles the onTouch events.

You'll have to add the special funcionality you're looking for at this part of the code (specially, the ACTION_DOWN) event:

@Override
    public final boolean onTouch(View v, MotionEvent ev) {
        boolean handled = false;

        if (mZoomEnabled && hasDrawable((ImageView) v)) {
            ViewParent parent = v.getParent();
            switch (ev.getAction()) {
                case ACTION_DOWN:
                    // First, disable the Parent from intercepting the touch
                    // event
                    if (null != parent)
                        parent.requestDisallowInterceptTouchEvent(true);
                    else
                        Log.i(LOG_TAG, "onTouch getParent() returned null");

                    // If we're flinging, and the user presses down, cancel
                    // fling
                    cancelFling();
                    break;

                case ACTION_CANCEL:
                case ACTION_UP:
                    // If the user has zoomed less than min scale, zoom back
                    // to min scale
                    if (getScale() < mMinScale) {
                        RectF rect = getDisplayRect();
                        if (null != rect) {
                            v.post(new AnimatedZoomRunnable(getScale(), mMinScale,
                                    rect.centerX(), rect.centerY()));
                            handled = true;
                        }
                    }
                    break;
            }

            // Check to see if the user double tapped
            if (null != mGestureDetector && mGestureDetector.onTouchEvent(ev)) {
                handled = true;
            }

            if (!handled && null != parent) {
                parent.requestDisallowInterceptTouchEvent(false);
            }

            // Finally, try the Scale/Drag detector
            if (null != mScaleDragDetector
                    && mScaleDragDetector.onTouchEvent(ev)) {
                handled = true;
            }
        }

        return handled;
    }
like image 43
Henrique Avatar answered Oct 22 '22 12:10

Henrique