在开发过程中都会有点击图片进行图片放大并且轮滑显示效果,
1、导入jar包
compile files('libs/uk-co-senab-photoview.jar')
2、布局
<?xml version="1.0" encoding="utf-8"?> <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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.macbook_.headline.activity.ImageShowActivity"> <android.support.v4.view.ViewPager android:id="@+id/photo_vp" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>3、代码
public class ImageShowActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_show); ViewPager vp = (ViewPager) findViewById(R.id.photo_vp); //获取数据 Intent intent = getIntent(); int position = intent.getIntExtra("id", 0); List<Home_Dtail.Home_Image> images = (List<Home_Dtail.Home_Image>) intent.getSerializableExtra("images"); ImageShowAdapter adapter = new ImageShowAdapter(images,this); vp.setAdapter(adapter); vp.setCurrentItem(position); } }4、适配器
public class ImageShowAdapter extends PagerAdapter { private List<Home_Dtail.Home_Image> images; private Activity context; public ImageShowAdapter(List<Home_Dtail.Home_Image> images, Activity context) { this.images = images; this.context = context; } @Override public int getCount() { return images.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { PhotoView photoView=new PhotoView(context); Glide.with(context).load(images.get(position).getUrl()).into(photoView); photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() { @Override public void onPhotoTap(View view, float v, float v1) { context.finish(); } }); container.addView(photoView); return photoView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }