Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Glide to load a Placeholder from URL to display while loading a GIF (Android)

What I have is this:

Glide
            .with(this)
            .load(imageUrl)
            .asGif()
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .placeholder(R.drawable.gif)
            .into(imageView);

But instead, I want to use Glide to load the same gif asBitmap() to use as placeholder for while it's loading the actual gif.

Like if I could do: .placeholder(Glide.with(this).load(imageUrl).asBitmap())

like image 957
Gianfranco Gasbarri Avatar asked Jul 07 '17 02:07

Gianfranco Gasbarri


1 Answers

You have to pass the URL in .thumbnail(url) as

.thumbnail(Glide
        .with(context)
        .load(Url)
        .asBitmap()

Or like this:-

DrawableRequestBuilder<String> thumbnail = Glide.with(context)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .load(url);
    try {
        Glide.with(context)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(placeholder)
                .load(url)
                .thumbnail(thumbnail)
                .into(imageView);
    } catch (Exception e) {
        e.printStackTrace();
    }

Reference:

https://github.com/bumptech/glide/issues/1198

https://futurestud.io/tutorials/glide-thumbnails

https://github.com/bumptech/glide/issues/362

private void loadImage(ImageView image, @RawRes int typeID, String imagePath) {
Context context = image.getContext();
BitmapPool pool = Glide.get(context).getBitmapPool();

// OPTION 1 Bitmap
Glide
    .with(image.getContext())
    .load(imagePath)
    .asBitmap()
    .animate(android.R.anim.fade_in)
    .placeholder(R.drawable.image_loading)
    .error(R.drawable.image_error)
    .thumbnail(Glide
        .with(context)
        .load(typeID)
        .asBitmap()
        .imageDecoder(new SvgBitmapDecoder(pool)) // implements ResourceDecoder<InputStream, Bitmap>
    )
    .into(image)
;

// OPTION 2 GlideDrawable
Glide
    .with(image.getContext())
    .load(imagePath)
    .crossFade()
    .placeholder(R.drawable.image_loading)
    .error(R.drawable.image_error)
    .thumbnail(Glide
        .with(context)
        .load(typeID)
        .decoder(new GifBitmapWrapperResourceDecoder(
                    new ImageVideoBitmapDecoder(
                        new SvgBitmapDecoder(pool),
                        null /*fileDescriptorDecoder*/
                    ),
                    // just to satisfy GifBitmapWrapperResourceDecoder.getId() which throws NPE otherwise
                    new GifResourceDecoder(context, pool),
                    pool
                )
        )
    )
    .into(image)
;
}
like image 193
Ankit Kumar Avatar answered Oct 11 '22 12:10

Ankit Kumar