@Override
public void onPageSelected(int position) {
//获取子页面数量
int childCount=mViewPager.getChildCount();
for (int i=0;i<childCount;i++){
//获取当前页面的view
View child=mViewPager.getChildAt(i);
//获取当前页面中的PhotoView
PhotoView photoView=child.findViewById(R.id.prePhotoView);
if (photoView != null) {
//获取photoView创建的PhotoViewAttacher
PhotoViewAttacher photoViewAttacher= (PhotoViewAttacher) photoView.getIPhotoViewImplementation();
//通过photoViewAttacher设置缩放大小
//第一个参数是获取photoViewAttacher自带的缩放大小最小值,第二个和第三个参数设置缩放中心
photoViewAttacher.setScale(photoViewAttacher.getMinimumScale(), 0f, 0f, true);
}
}
}
PhotoView使用:
引入依赖:
implementation 'com.github.chrisbanes:PhotoView:1.3.1'
build.gradle中加入:
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
添加布局:
<uk.co.senab.photoview.PhotoView
android:id="@+id/photoView"
android:layout_width="180dp"
android:layout_height="140dp"
android:background="#333333"
android:layout_marginLeft="20dp"
/>
设置点击事件:
photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
@Override
public void onPhotoTap(View arg0, float arg1, float arg2) {
}
@Override
public void onOutsidePhotoTap() {
}
});
网友评论