图片的放大缩小实现效果是使用的github上的一个开源项目photoView实现的,下载地址: https://github.com/chrisbanes/PhotoView
下面看测试代码:
activity_main.xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <android.support.v4.view.ViewPager 7 android:id="@+id/viewPager" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" > 10 </android.support.v4.view.ViewPager> 11 12 </RelativeLayout>
MainActivity.java:
1 package com.zzw.tetsphotoview; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import uk.co.senab.photoview.PhotoViewAttacher; 7 8 import android.app.Activity; 9 import android.content.Context; 10 import android.os.Bundle; 11 import android.os.Handler; 12 import android.os.Message; 13 import android.support.v4.view.PagerAdapter; 14 import android.support.v4.view.ViewPager; 15 import android.view.View; 16 import android.view.ViewGroup; 17 import android.widget.ImageView; 18 19 public class MainActivity extends Activity { 20 ViewPager mViewPager; 21 List<ImageView> imageViews; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 mViewPager = (ViewPager) findViewById(R.id.viewPager); 28 mViewPager.setAdapter(new ImageAdapter(this)); 29 } 30 31 class ImageAdapter extends PagerAdapter { 32 Context context; 33 int[] images; 34 35 public void init() { 36 imageViews = new ArrayList<ImageView>(); 37 images = new int[] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d }; 38 for (int i = 0; i < images.length; i++) { 39 ImageView image = new ImageView(context); 40 image.setImageResource(images[i]); 41 42 //使图片实现可以放大缩小的功能 43 PhotoViewAttacher mAttacher=new PhotoViewAttacher(image); 44 45 46 imageViews.add(image); 47 48 49 } 50 // for (int i : images) { 51 // ImageView image = new ImageView(context); 52 // image.setImageResource(i); 53 // imageViews.add(image); 54 // } 55 } 56 57 public ImageAdapter(Context context) { 58 this.context = context; 59 init(); 60 } 61 62 @Override 63 public void destroyItem(ViewGroup container, int position, Object object) { 64 container.removeView(imageViews.get(position)); 65 } 66 67 @Override 68 public View instantiateItem(ViewGroup container, int position) { 69 container.addView(imageViews.get(position)); 70 71 return imageViews.get(position); 72 } 73 74 @Override 75 public int getCount() { 76 return imageViews.size(); 77 } 78 79 @Override 80 public boolean isViewFromObject(View arg0, Object arg1) { 81 return arg0 == arg1; 82 } 83 84 } 85 86 }