glide icon indicating copy to clipboard operation
glide copied to clipboard

Photoview callback upon completion of image loading?

Open danieljonker opened this issue 10 years ago • 7 comments

Upon switching from Picasso to Glide and using Chris Banes' Photoview library, I can't get the same functionality.

So we have this bug, with photoview in our app, where we used the solution provided with Picasso: https://github.com/chrisbanes/PhotoView/issues/99

But the .listener doesn't seem to work the same as the calback in picasso. Meaning, this code snippet:

public void loadImageInView(String imageUrl, ImageView imageView, ImageType imageType, final PhotoViewAttacher attacher) {
    mGlideBridge
        .with(imageView.getContext())
        .load(getTemplatedUrl(imageUrl, mImageSizes.get(imageType)[0], mImageSizes.get(imageType)[1]))
        .placeholder(getPlaceholderImageDrawable(mImageSizes.get(imageType)[2]))
        .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) {
                if (attacher != null) {
                    Timber.d("resourceReady");
                    attacher.update();
                }
                return false;
            }
        })
        .into(imageView);
}

doesn't work. But this (with Picasso) does:

public void loadImageInView(String imageUrl, ImageView imageView, ImageType imageType, final PhotoViewAttacher attacher) {
    picasso
        .load(getTemplatedUrl(imageUrl, mImageSizes.get(imageType)[0], mImageSizes.get(imageType)[1]))
        .placeholder(getPlaceholderImageDrawable(mImageSizes.get(imageType)[2]))
        .into(imageView, new Callback() {
            @Override
            public void onSuccess() {
                if (attacher != null)
                    attacher.update();
            }

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

I can't seem to find any other way of listening for the image to have finished loading.

danieljonker avatar Feb 09 '16 16:02 danieljonker

As far as I remember Picasso loads the full size image all the time, while Glide tries to load an image Bitmap that is a perfect fit for the view to save memory and processing power. This means that Glide will wait for a layout to happen: no layout -> no size -> Glide cannot start loading -> listener not called.

This is my guess for what's happening here, because your usage of Glide seems correct. One way of working around this is to provide Glide with a size to override the wait for first layout, try:

.override(mImageSizes.get(imageType)[0], mImageSizes.get(imageType)[1]) // assuming those [0]/[1] are width/height.

If it doesn't work please take a look at the wiki to go deeper and see what's going on.

I can't seem to find any other way of listening for the image to have finished loading.

You can also implement a Target, see GlideDrawableImageViewTarget (what you're currently using) and its super classes.


Usage tips: There's a way to hide the template url resolution by registering custom models (e.g. class Image { String imageUrl, ImageType type; }) and building the Url inside a BaseGlideUrlLoader subclass. Glide also supports loading the drawable via a resource ID, mImageSizes.get(imageType)[2] looks like an int.

TWiStErRob avatar Feb 09 '16 17:02 TWiStErRob

So I've fixed my issue, it wasn't that the listener callbacks weren't being triggered, it just wasn't working with photoview properly. So basically when setting up the photoview I disabled zoom, then in resource ready, re-enabled it.

danieljonker avatar Feb 11 '16 11:02 danieljonker

If you want to zoom an image you need to tell Glide to load a bigger image, otherwise it'll be blurry: .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).

TWiStErRob avatar Feb 11 '16 11:02 TWiStErRob

I've adjusted the request to:

mGlideBridge.with(imageView.getContext()).load(getTemplatedUrl(imageUrl, mImageSizes.get(imageType)[0], mImageSizes.get(imageType)[1]))
                .override(mImageSizes.get(imageType)[0], mImageSizes.get(imageType)[1])
                .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);

But like you said it's still blurry, am I using the wrong .override method? Should I being calling .into(Target)?

danieljonker avatar Feb 11 '16 11:02 danieljonker

The image will have the size at most what is defined by mImageSizes.get(imageType), so if that's small, you get a blurry image; try with SIZE_ORIGINAL. I don't think you need a custom target for this.

TWiStErRob avatar Feb 11 '16 11:02 TWiStErRob

Ok, cool. Got it sorted. Thanks

danieljonker avatar Feb 11 '16 11:02 danieljonker

@TWiStErRob thanks for giving a clue on the issue. I can confirm: as soon as I started using .override() the issue has disappeared.

Pynop avatar Aug 09 '17 19:08 Pynop