0

在将 viewpager 与 picasso 库和 photoview 库一起使用时,我面临两个问题。

1)一些图像不缩放,而有时下一个图像被缩放。

2)有时当我缩放图像时,相邻图像也会与当前图像一起缩放。

如何解决以上2个问题?

public class ImageViewerPagerAdapter extends PagerAdapter implements PhotoViewAttacher.OnViewTapListener {


Context context;
private int count;
private ArrayList<String> images;
private PhotoViewAttacher mAttacher;
private boolean shouldFit = false;


public ImageViewerPagerAdapter(Context context, int count, ArrayList<String> images, boolean shouldFit) {
    this.context = context;
    this.count = count;
    this.images = new ArrayList<>(images);
    this.shouldFit = shouldFit;

}


@Override
public int getCount() {
    return count;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}


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


@Override
public Object instantiateItem(ViewGroup container, int position) {

    String imageUrl = images.get(position);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.activity_image_viewpager_layout, container, false);
    final PhotoView imageView = (PhotoView) view.findViewById(R.id.image);
    if (shouldFit) {
        Picasso.with(context).load(imageUrl).into(imageView, new Callback() {
            @Override
            public void onSuccess() {

                if (mAttacher != null) {
                    mAttacher.update();
                } else {
                    mAttacher = new PhotoViewAttacher(imageView);
                    mAttacher.setOnViewTapListener(ImageViewerPagerAdapter.this);
                }

            }

            @Override
            public void onError() {

            }
        });
    } else {
        Picasso.with(context).load(imageUrl).into(imageView, new Callback() {
            @Override
            public void onSuccess() {
                if (mAttacher != null) {
                    mAttacher.update();
                } else {
                    mAttacher = new PhotoViewAttacher(imageView);
                    mAttacher.setOnViewTapListener(ImageViewerPagerAdapter.this);
                }
            }

            @Override
            public void onError() {

            }
        });
    }

    container.addView(view);


    return view;
}


@Override
public void onViewTap(View view, float v, float v1) {
    Toaster.make(context,"hello");
    ((ImageViewPagerActivity) context).animateToolbar();
}


}
4

1 回答 1

0

安卓操作系统的问题。当操作系统绘制视图时,它会将其缓存在系统中,当系统请求相同的视图时,操作系统不会绘制新视图,而是从缓存中检索已经绘制的视图。这就是为什么您会遇到下一张图像显示在视图中的问题。您也可以在列表中看到这样的问题

于 2017-03-21T10:44:07.677 回答