Search code examples
androidzoomingandroid-viewpager

Android ViewPager with zoomable ImageView


I've got a ViewPager made by ImageViews implemented like this:

@Override
    public Object instantiateItem(View collection, int position) {
        ImageView iv = new ImageView(ctx);
        if (pageList.size() > position)
            iv.setImageBitmap(pageList.get(position));
        else
            iv.setImageResource(R.drawable.loading);

        iv.setOnTouchListener(this);
        ((ViewPager) collection).addView(iv, 0);
        System.out.println("POS: " + position);
        return iv;
    }

Any chance i can have that ImageView zoomable by double tap (and swipe the image) or pinch zoomable?


Solution

  • It's possible, here's how I did it.

    My top level layout element is FrameLayout which allows several views to be stacked on top of each other. The FrameLayout has two children: the ViewPager and an ImageView that I'll use for showing zoomed-in picture.

    The ImageView is normally hidden. I listen for touch events on pictures inside the ViewPager and based on those, show and hide and pan the ImageView. It works fairly well. I can give more details if need be.