Photoview: ImageView inside ViewPager ignores layout params

Created on 24 Oct 2013  路  15Comments  路  Source: Baseflow/PhotoView

I have an ImageView in a fragment hosted by a view pager. The fragment layout is a RelativeLayout and the ImageView is centerPositioned inside of it. When I initialize the PhotoAttacher with the ImageView the image starts at the top of the screen instead of in the center of the screen. If I touch and drag a tiny bit in any direction the image immediately snaps into place. The image is loaded dynamically from the web if that is useful.

bug

Most helpful comment

On glide to get around this issue, the same solution didn't work with their listener callbacks. Instead I had to disable zoom, then re-enable it in the resourceready callback.

final PhotoViewAttacher attacher = new PhotoViewAttacher(imageView);
attacher.setZoomable(false);
imageHelper.loadImageInView(url, imageView, imageType, attacher);
Glide.with(imageView.getContext()).load(url)
                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        attacher.setZoomable(true);
                        return false;
                    }
                })
                .into(imageView);

All 15 comments

I find this bug now

How can I fix it?

yes,I also found this issue

Same here !

same here, cant understand how to solve this issue. In my case the Image is displayed near bottom right corner, no matter any size.

however if I disable zoom, the Image is properly displayed in the center of the Screen

Hope this helps somebody...

I had this same problem when using Picasso. The solution for me was to make sure PhotoViewAttacher#update() is called after the image is done loading.

Eg, converted this:

Picasso.with(context)
    .load(url)
    .fit()
    .centerInside()
    .into(imageView);

new PhotoViewAttacher(imageView);

To this:

final PhotoViewAttacher photoViewAttacher = new PhotoViewAttacher(imageView);

Picasso.with(context)
    .load(url)
    .fit()
    .centerInside()
    .into(imageView, new Callback() {
        @Override
        public void onSuccess() {
            photoViewAttacher.update();
        }

        @Override
        public void onError() { }
    });

@brentwatson Good catch and solution there. Might be a good thing to consider putting in the README.

On glide to get around this issue, the same solution didn't work with their listener callbacks. Instead I had to disable zoom, then re-enable it in the resourceready callback.

final PhotoViewAttacher attacher = new PhotoViewAttacher(imageView);
attacher.setZoomable(false);
imageHelper.loadImageInView(url, imageView, imageType, attacher);
Glide.with(imageView.getContext()).load(url)
                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        attacher.setZoomable(true);
                        return false;
                    }
                })
                .into(imageView);

@danieljonker great find on Glide! Worked like a charm!

@JAgostoni I just edited the above code snippet, you also need to tell glide to request the original sized image with:
.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)

@danieljonker thanks. I added that and did notice any changes. What did that do for you?

@JAgostoni Glide downsizes the image based on your imageview size, So if you're wanting to zoom in with Photoview your image will be as big as the imageview. So zooming will cause it to be blurry.

Thanks, @danieljonker. Probably just coincidental that my images were sized close enough!

Added to the sample. Thanks!

@danieljonker Thanks you saved my day!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

feresr picture feresr  路  5Comments

nithinpmolethu picture nithinpmolethu  路  11Comments

macpraveen picture macpraveen  路  4Comments

FaultException picture FaultException  路  9Comments

anthony3444 picture anthony3444  路  9Comments