jsibbold / zoomage

A simple pinch-to-zoom ImageView library for Android
http://jsibbold.github.io/zoomage
Apache License 2.0
315 stars 76 forks source link

Can't swipe ViewPager #77

Closed adeldolgov closed 5 years ago

adeldolgov commented 5 years ago

After update to 1.3.0 i can't swipe to left right inside viewpager. How can i fix it? I can't change images

jsibbold commented 5 years ago

If you're swiping on the actual image, it's going to prioritize translating and/or zooming the image over swiping pages.

adeldolgov commented 5 years ago

Then how can i swipe horizontally, if i have full match parent zoomageView?

adeldolgov commented 5 years ago

Also is there any zoom listener avalaible?

jsibbold commented 5 years ago

Can you check to see if it works by setting translatable to false?

adeldolgov commented 5 years ago

It works, but then i can't swipe on image

adeldolgov commented 5 years ago

If you have zoom listener, then i can just disable ViewPager paging, and reenable when picture zoomout

jsibbold commented 5 years ago

currently there isn't but you could override onTouchEvent and check the current zoom factor there and make your calls, then return super.onTouchEvent

jsibbold commented 5 years ago

A zoom listener has been requested and I am planning to add it sometime soon though. In the mean time I would try the suggestion above.

adeldolgov commented 5 years ago

How can i check inside onTouchEvent if image was zoomed?

jsibbold commented 5 years ago

getCurrentScaleFactor

adeldolgov commented 5 years ago

Thanks! I fixed the issue.

adeldolgov commented 5 years ago

! version 1.2.0 implementation 'com.jsibbold:zoomage:1.2.0'

Thanks @jsibbold for help!

Let me explain how i fixed it : Inside your CustomPagerAdapter :

OnImageZoomListener listener; 

 public void setOnImageZoomListener(OnImageZoomListener listener) {
        this.listener = listener;
    }

public interface OnImageZoomListener
    {
        void onImageZoomed(boolean isZoomed);
    }

Inside overrided instantiateItem method of CustomPagerAdapter

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {

        LayoutInflater inflater = LayoutInflater.from(mContext);
        ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.item_galerry_detail, collection, false);
        ZoomageView zoomageView = layout.findViewById(R.id.zoomageView);
        zoomageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(zoomageView.getCurrentScaleFactor()>1f || zoomageView.getCurrentScaleFactor()<1f)
                    listener.onImageZoomed(true);
                else
                    listener.onImageZoomed(false);
                return zoomageView.onTouchEvent(event);
            }
        });
}

Then on Activity/Fragment

CustomPagerAdapter customPagerAdapter;
customPagerAdapter.setOnImageZoomListener(new CustomPagerAdapter.OnImageZoomListener() {
            @Override
            public void onImageZoomed(boolean isZoomed) {
                if(isZoomed)
                    customViewPager.setPagingEnabled(false);
                else
                    customViewPager.setPagingEnabled(true);
            }
        });
jsibbold commented 5 years ago

Glad you got it working, it's a difficult thing figuring out how to prioritize overlapped touchable views. I may consider adding a bit of logic specifically for views that have not been zoomed at all, for translation. The only potential problem I see there is if someone has centerCropped an image and it's bigger than the visible area, they wouldn't be able to translate and may want to.

adeldolgov commented 5 years ago

Okay. Thank you for the library!

adeldolgov commented 5 years ago

@jsibbold this solution allows to move picture left/right sides when picture zoomed inside ViewPager item. Without this when you using horizontal scrolling it will change picture (or ViewPager item), not move zoom. You can tag it somehow if you want, maybe this will be helpful for some users.

parthanjaria commented 5 years ago

Awesome solution. Just one change now while using viewpager2(new component) is :

zoomageView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if(zoomageView.getCurrentScaleFactor()>1f || zoomageView.getCurrentScaleFactor()<1f) {
                        listener.onImageZoomed(true);
                        zoomageView.setTranslatable(true);
                    }
                    else {
                        listener.onImageZoomed(false);
                        zoomageView.setTranslatable(false);
                    }

                    return zoomageView.onTouchEvent(motionEvent);
                }
            });
jsibbold commented 5 years ago

I'll work on a solution for this problem in the next snapshot. I would want to add some options for it so devs can decide whether they want this behavior or not.