使用Glide从URL加载占位符以在加载GIF时显示(Android) [英] Using Glide to load a Placeholder from URL to display while loading a GIF (Android)

查看:502
本文介绍了使用Glide从URL加载占位符以在加载GIF时显示(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么?

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

但是,相反,我想使用Glide加载与Bitmap()相同的gif,以便在加载实际gif时用作占位符.

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.

就像我能做到的那样:.placeholder(Glide.with(this).load(imageUrl).asBitmap())

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

推荐答案

您必须以

的方式在 .thumbnail(url)中传递URL.

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

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

或者这样:-

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();
    }

参考:

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)
;
}

这篇关于使用Glide从URL加载占位符以在加载GIF时显示(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆