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

标签 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())

最佳答案

您必须将 .thumbnail(url) 中的 URL 传递为

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

关于android - 使用 Glide 从 URL 加载占位符以在加载 GIF 时显示 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44961710/

相关文章:

java - 在 fragment 中使用网络 ImageView - android studio

Android:从压力传感器获取高度

android - 使用 tablayout 切换 fragment 时重新创建所有 fragment

Android EditText 文本控件 - 如何禁用从 Google 键盘插入 GIF 的可能性?

android - 带有 StaggeredGridLayoutManager 的 RecyclerView,图像从 Glide 加载

c# - 如何将流中的图像另存为在 retrofit2 中发送到 wcf web 服务

javascript - 动画 GIF 作为 THREE.js 中的纹理

css - 录制 SVG 动画并将其保存为动画 GIF

android - RecyclerView 回收 ViewHolder Image View 尺寸错误

java - Glide OnResource设置Call​​back类成员变量以供复用