使用Glide从URL加载占位符,以便在加载GIF时显示(Android)

时间:2017-07-07 02:39:52

标签: android gif android-glide

我拥有的是:

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

但相反,我想使用Glide加载相同的gif asBitmap()作为占位符,同时加载实际的gif。

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

1 个答案:

答案 0 :(得分:7)

您必须将 .thumbnail(网址)中的网址作为

传递
.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)
;
}