美文网首页
PhotoView+Viewpager切换下一页前一页恢复原本大

PhotoView+Viewpager切换下一页前一页恢复原本大

作者: jjswer | 来源:发表于2018-07-31 14:42 被阅读0次
                @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() {
                    }
                });
    

    相关文章

      网友评论

          本文标题:PhotoView+Viewpager切换下一页前一页恢复原本大

          本文链接:https://www.haomeiwen.com/subject/htchvftx.html