这里主要还是用到了Android自带的ViewPager控件,用于实现水平切换,并且它自带了滑动效果。将每一张需要展示的图片放在独立的Fragment中,ViewPager每滑动一次,展示新的Fragment。
1.设计Fragment的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_main_pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
/>
</LinearLayout>
2.建立自定义的PictureSlideFragment类继承自Fragment
由于每一个Fragment展示的图片不同,因此需要在创建PictureSlideFragment时传入需要加载的图片url。添加静态方法newInstance()获得一个包含图片url的PictureSlideFragmen的实例。接着在其执行onCreate()方法时,对url赋值,接着就能在onCreateView()方法中通过findViewById()找到ImageView的实例,并将其图片来源设置为指定的url.这里使用强大的Glide工具为ImageView加载网络图片。Glide的load()方法有很多重载,可以直接传入drawable的ResourceId加载本地图片,也可以传入url加载网络图片。此时,Fragment中显示的ImageView还是静态的,它不可移动,不可缩放,显然不符合查看高清大图时的需求。因此在使用Glide为ImageView填充图片之前,我们使用PhotoViewAttacher为图片添加支持缩放、平移的属性。至此,应该能符合我们的要求了,但是由于使用了PhotoViewAttacher,它