8

I am using ViewPager to retrieve images from remote server. Everything is working well, but the problem is I don't know how to implement pinch Zoom in/out function to it. My code is below

public class ImagePagerActivity extends BaseActivity {

    private ViewPager pager;

    private DisplayImageOptions options;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.ac_image_pager);
    Bundle bundle = getIntent().getExtras();
    String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
    int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

    options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.image_for_empty_url)
                .cacheOnDisc()
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                .build();

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(new ImagePagerAdapter(imageUrls));
    pager.setCurrentItem(pagerPosition);

    }

    protected void onStop() {
    imageLoader.stop();
    super.onStop();
    }

    private class ImagePagerAdapter extends PagerAdapter {

    private String[] images;
    private LayoutInflater inflater;

    ImagePagerAdapter(String[] images) {
    this.images = images;
    inflater = getLayoutInflater();
    }

    public void destroyItem(View container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
    }

    public void finishUpdate(View container) {
    }

    public int getCount() {
    return images.length;
    }

    public Object instantiateItem(View view, int position) {
                final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
                final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
                final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

    imageLoader.displayImage(images[position], imageView, options, new ImageLoadingListener() {

    public void onLoadingStarted() {
    spinner.setVisibility(View.VISIBLE);
    }


    public void onLoadingFailed(FailReason failReason) {
    String message = null;
    switch (failReason) {
    case IO_ERROR:
    message = "Input/Output error";
    break;
    case OUT_OF_MEMORY:
    message = "Out Of Memory error";
    break;
    case UNKNOWN:
    message = "Unknown error";
    break;
    }
    Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

    spinner.setVisibility(View.GONE);
                              imageView.setImageResource(android.R.drawable.ic_delete);
                    }


    public void onLoadingComplete(Bitmap loadedImage) {
    spinner.setVisibility(View.GONE);
    Animation anim = AnimationUtils.loadAnimation(ImagePagerActivity.this, R.anim.fade_in);
    imageView.setAnimation(anim);
    anim.start();
    }

    public void onLoadingCancelled() {
    // Do nothing
    }
    });

    ((ViewPager) view).addView(imageLayout, 0);
    return imageLayout;
    }

    public boolean isViewFromObject(View view, Object object) {
    return view.equals(object);
    }

    public void restoreState(Parcelable state, ClassLoader loader) {
    }
    public Parcelable saveState() {
    return null;
    }

    public void startUpdate(View container) {
    }
    }
    }
Kuwame Brown
  • 533
  • 1
  • 10
  • 22
  • 1
    This seems like a basic function that should be inherited if there is an image. Don't understand why google hasn't made this available as an option in an image view – JPM Dec 19 '13 at 18:23

1 Answers1

6

if your viewpager is using an imageView then you can use touchImageView

https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/example/touch/TouchImageView.java

just replace your current imageview with this custom view. goodluck

WillingLearner
  • 739
  • 6
  • 10
  • Thanks, that was quite helpful :) Will try and update you result. – abhy Dec 06 '12 at 10:46
  • 5
    This seems to mostly work, though the pager's action seems to be fighting the touchimageview's actions, and trying to pinch-zoom will also start turning the page. And once the page starts turning, the pinch-zoom action ends. Requiring multiple careful attempts to really successfully zoom. – directedition Dec 09 '12 at 21:35
  • It is not zooming like full screen. But the zoom controls works fine. Can you help me how to zoom it with full screen ? – Sampath Kumar Jul 19 '13 at 06:13
  • 1
    the mentioned link is not working can someone please update the link – Erum May 12 '14 at 17:18