Image Gallery with Zoom and Pinch
Now I am going to explain, how to implement image gallery with zoom and pinch function with an example.
1. Create a new project in the Eclipse IDE from File ⇒ Android Application Project and fill all required details. I named my main activity as MainActivity.java
In MainActivity.java
package com.androidtutorialpoint; import com.androidtutorialpoint.imageview.PhotoView; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; public class MainActivity extends Activity { private ViewPager mViewPager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mViewPager = new HackyViewPager(this); setContentView(mViewPager); mViewPager.setAdapter(new SamplePagerAdapter()); } static class SamplePagerAdapter extends PagerAdapter { private static int[] sDrawables = { R.drawable.ic_launcher,R.drawable.ic_launcher, }; @Override public int getCount() { return sDrawables.length; } @Override public View instantiateItem(ViewGroup container, int position) { PhotoView photoView = new PhotoView(container.getContext()); photoView.setImageResource(sDrawables[position]); // Now just add PhotoView to ViewPager and return it container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); return photoView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } } }
2. In order to create an Pinch and zoom function, have to add supporting class files and library files such as
Create a class and named it has HackyViewPager
In HackyViewPager.java
package com.androidtutorialpoint; import android.content.Context; import android.support.v4.view.ViewPager; import android.view.MotionEvent; public class HackyViewPager extends ViewPager { public HackyViewPager(Context context) { super(context); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { try { return super.onInterceptTouchEvent(ev); } catch (IllegalArgumentException e) { e.printStackTrace(); return false; } } }
Finally add your library file .
Check output in device not in emulator
Download Source code here
can someone please help me i have two buttons on top and in center i have imageview and in footer i have again 3 buttons now center imageview will contain different image for every pager for page1 there is image1 and for page2 there is page2 and now for every image these top buttons and footer buttons should be used for zoomin and zoomout now my question is where should i write button events in java.may i need to write in this method :
ReplyDelete@Override
public View instantiateItem(ViewGroup container, int position) {
PhotoView photoView = new PhotoView(container.getContext());
can someone please help me for zoomin and zoomout i am using TouchImageView
Thank you! This was really helpful :)
ReplyDelete