图片放大缩小(和ViewPager配合使用流畅显示)--第三方开源--PhotoView

ygfb 9年前

 

图片放大缩小(和ViewPager配合使用流畅显示)--第三方开源--PhotoView

图片的放大缩小实现效果是使用的github上的一个开源项目photoView实现的,下载地址: https://github.com/chrisbanes/PhotoView

下面看测试代码:

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent" >        <android.support.v4.view.ViewPager          android:id="@+id/viewPager"          android:layout_width="match_parent"          android:layout_height="match_parent" >      </android.support.v4.view.ViewPager>    </RelativeLayout>

MainActivity.java:

package com.zzw.tetsphotoview;    import java.util.ArrayList;  import java.util.List;    import uk.co.senab.photoview.PhotoViewAttacher;    import android.app.Activity;  import android.content.Context;  import android.os.Bundle;  import android.os.Handler;  import android.os.Message;  import android.support.v4.view.PagerAdapter;  import android.support.v4.view.ViewPager;  import android.view.View;  import android.view.ViewGroup;  import android.widget.ImageView;    public class MainActivity extends Activity {      ViewPager mViewPager;      List<ImageView> imageViews;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          mViewPager = (ViewPager) findViewById(R.id.viewPager);          mViewPager.setAdapter(new ImageAdapter(this));      }        class ImageAdapter extends PagerAdapter {          Context context;          int[] images;            public void init() {              imageViews = new ArrayList<ImageView>();              images = new int[] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d };              for (int i = 0; i < images.length; i++) {                  ImageView image = new ImageView(context);                  image.setImageResource(images[i]);                                    //使图片实现可以放大缩小的功能                  PhotoViewAttacher mAttacher=new PhotoViewAttacher(image);                                                      imageViews.add(image);                                         }  //            for (int i : images) {  //                ImageView image = new ImageView(context);  //                image.setImageResource(i);  //                imageViews.add(image);  //             }          }            public ImageAdapter(Context context) {              this.context = context;              init();          }            @Override          public void destroyItem(ViewGroup container, int position, Object object) {              container.removeView(imageViews.get(position));          }            @Override          public View instantiateItem(ViewGroup container, int position) {              container.addView(imageViews.get(position));                return imageViews.get(position);          }            @Override          public int getCount() {              return imageViews.size();          }            @Override          public boolean isViewFromObject(View arg0, Object arg1) {              return arg0 == arg1;          }        }    }