Zoom and swipe issue with ViewPager in android

707 views Asked by At

I am new in android development. I am trying to make a wallpaper application where need to integrate images swipe and pinch zoom functionality.

I had done both but not at a time. When I integrate swipe its work but when I integrate pinch zoom then not worked it. Both functionality work alone but not together.

I tried to find solutions many times but not get from any where. Below code used.

public class FullImageActivity extends Activity {
    protected int curruntPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_image);

        ImageAdapter imageAdapter = new ImageAdapter(this);
        List<ImageView> images = new ArrayList<ImageView>();
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

        for (int i = 0; i < imageAdapter.getCount(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(imageAdapter.mThumbIds[i]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            images.add(imageView);


        }
        ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(images);
        viewPager.setAdapter(imagePagerAdapter);
        int position = getIntent().getExtras().getInt("id");
        viewPager.setCurrentItem(position);

         viewPager.setOnPageChangeListener(new OnPageChangeListener() {

              @Override public void onPageSelected(int arg0)
              { // TODOAuto-generated method stub // Here you can set the wallpaper
              curruntPosition = arg0;

              }

              @Override public void onPageScrolled(int arg0, float arg1, int arg2)
              { // TODO Auto-generated method stub

              }

              @Override public void onPageScrollStateChanged(int arg0)
              { // TODO Auto-generated method stub

              }

              });


            TouchImageView img = new TouchImageView(this);
              img.setImageResource(imageAdapter.mThumbIds[position]);
              img.setMaxZoom(4f);
              setContentView(img);
    }
}
}

I used above code. Let me know if any one have a solution what actually issue with it.

thanks

2

There are 2 answers

1
Andrea Cinesi On

maybe you can try to avoid swiping when you have to pinch

public class CustomViewPager extends ViewPager { 
private boolean enabled; 
public CustomViewPager(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.enabled = true; 
} 
@Override 
public boolean onTouchEvent(MotionEvent event) { 
    if (this.enabled) { 
       return super.onTouchEvent(event); 
    } 
    return false; 
} 
@Override 
public boolean onInterceptTouchEvent(MotionEvent event) { 
    if (this.enabled) { 
        return super.onInterceptTouchEvent(event); 
    } 
    return false; 
} 
public void setPagingEnabled(boolean enabled) { 
    this.enabled = enabled; 
}
}

After doing this, the only thing left was to substitute the ViewPager tag on the layout file for my own class and then call the method setPagingEnabled false, hope it helps

0
Silvans Solanki On

For swipe you can measure minimum swipe distance and based on that you can identify zoom and swipe. Also you can have a look at the given url for reference to achieve your goal

http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/

Hope this help you.