Glide: Disable crossfade transition only to cached images

Created on 12 Jun 2017  路  4Comments  路  Source: bumptech/glide

Hi i'm using latest version of glide -
com.github.bumptech.glide:compiler:4.0.0-RC0

I'm to do as the title says, I've looked through the threads (which have amazing feedback from @TWiStErRob !) but still couldn't figure it out.

If the image aren't cached already on the device it's cool that it crossfades, but i'm not intrested in crossfading the ones that are already cached.

Here's my shot:

        RequestOptions options = new RequestOptions()
                    .fallback(R.drawable.icon)
                    .error(R.drawable.icon)
                    .dontTransform()
                    .diskCacheStrategy(DiskCacheStrategy.ALL);

        RequestOptions thumbnailOptions = new RequestOptions()
                .bitmapTransform(new BlurTransformation(imageView.getContext()));


        RequestBuilder requestBuilder = Glide
                .with(imageView.getContext())
                .load(thumbnailUrl)
                .apply(thumbnailOptions);

        DrawableTransitionOptions drawableTransitionOptions = imageView.getDrawable() == null ? withCrossFade() : withCrossFade().dontTransition();

        Glide.with(imageView.getContext())
                .load(url)
                .apply(options)
                .thumbnail(requestBuilder)
                .transition(drawableTransitionOptions)
                .into(imageView);

Thanks !

documentation question v4

Most helpful comment

@sjudd This information is so useful!! I don't know if it is in the official document already. If not I think it is worth adding this. I believe many people struggling with where is the right place to apply animation strategy. This is correct and easy way to determine if want to use transition based on DataSource.
I used it in my code and works.
Thanks.

.transition(DrawableTransitionOptions.with(new TransitionFactory<Drawable>() {
    @Override
    public Transition<Drawable> build(DataSource dataSource, boolean isFirstResource) {
        if (dataSource == DataSource.RESOURCE_DISK_CACHE) 
            return null;        
        return new DrawableCrossFadeFactory.Builder(1000).build().build(dataSource, isFirstResource);
    }
}))

All 4 comments

By cached, do you mean cached on disk or in memory?

If images are cached in memory, by default, Glide will not animate the image when loading it. If images are cached on disk, Glide will animate. To change that, define a custom TransitionFactory and pass it in with DrawableTransitionOptions#with. In TransitionFactory, you can inspect DataSource and avoid returning transitions for images that are loaded from disk caches.

For an example implementation, see the cross fade factory: https://github.com/bumptech/glide/blob/master/library/src/main/java/com/bumptech/glide/request/transition/DrawableCrossFadeFactory.java

@sjudd Thanks for your answer. I will try to write your solution, if it works out for me, I'll share it here.

@sjudd This information is so useful!! I don't know if it is in the official document already. If not I think it is worth adding this. I believe many people struggling with where is the right place to apply animation strategy. This is correct and easy way to determine if want to use transition based on DataSource.
I used it in my code and works.
Thanks.

.transition(DrawableTransitionOptions.with(new TransitionFactory<Drawable>() {
    @Override
    public Transition<Drawable> build(DataSource dataSource, boolean isFirstResource) {
        if (dataSource == DataSource.RESOURCE_DISK_CACHE) 
            return null;        
        return new DrawableCrossFadeFactory.Builder(1000).build().build(dataSource, isFirstResource);
    }
}))

@zeroarst Thanks for sharing your solution, mine is quite the same, but when I tested it didn't work as i expected, this is my code:

 private static class CustomTransition implements TransitionFactory<Drawable>{

    @Override
    public Transition<Drawable> build(DataSource dataSource, boolean isFirstResource) {
        if(dataSource != DataSource.REMOTE){
            return NoTransition.get();
        }else{
            return new DrawableCrossFadeFactory.Builder().build().build(dataSource, isFirstResource);
        }
    }


}

I'll check yours to see if it reacts better.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings