Android 使用ViewPager实现图片左右滑动和PhotoView浏览大图

1.PhotoView的介绍:

PhotoView的github地址:
https://github.com/chrisbanes/PhotoView

PhotoView特性:

 支持单点/多点触摸,即时缩放图片;
 支持平滑滚动;
 在滑动父控件下能够运行良好;(例如:ViewPager)
 当用户的触点改变时可以触发通知。

PhotoView的详解,请参考PhotoView源码剖析

2.上Demo源码:

ViewPager的布局:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar_title"
        android:id="@+id/toolbar_title"/>

    <com.dgaotech.dgfw.widget.ImageViewPager
        android:id="@+id/img_viewpager"
        android:layout_below="@+id/toolbar_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"/>

    <TextView
        android:id="@+id/text_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="@dimen/public_margin20dp"
        android:textSize="@dimen/public_textsize20sp"
        android:textColor="@color/white"
        android:text="1/3"
        android:visibility="visible"/>

RelativeLayout>

adapter的布局:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <uk.co.senab.photoview.PhotoView
        android:id="@+id/photoview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

RelativeLayout>

主要核心代码:

 List listViews = new ArrayList();
        for (int i = 0; i < data.size(); i++) {
            View view = LayoutInflater.from(getApplicationContext()).inflate(
                    R.layout.adapter_viewpager, null);
            final PhotoView photoview = (PhotoView) view.findViewById(R.id.photoview);
            photoview.setAdjustViewBounds(true);
            photoview.setScaleType(ImageView.ScaleType.FIT_CENTER);

            String url = data.get(i).getImg_url();
            Glide.with(this)
                    .load(url)
                    .skipMemoryCache(true)//不缓存到内存
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .placeholder(R.drawable.gugong_details)
                    .error(R.drawable.gugong_details)
                    .into(photoview);
            listViews.add(view);

            PhotoPagerAdapter photoPagerAdapter = new PhotoPagerAdapter(listViews);
            img_viewpager.setAdapter(photoPagerAdapter);
            //通过viewPager.setCurrentItem来定义当前显示哪一个图片,position由上一个页面传过来
            img_viewpager.setCurrentItem(position);
            photoPagerAdapter.notifyDataSetChanged();

PhotoPagerAdapter的代码:

public class PhotoPagerAdapter extends PagerAdapter {
    private List list;
    public PhotoPagerAdapter(List list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(list.get(position));
        return list.get(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}

以上就是ViewPager点击图片浏览大图且可以左右滑动的实现过程,有什么疑问的可以发邮件问我[email protected]

你可能感兴趣的:(Android)