使用Glide预加载多个图像 [英] Preload multiple images with Glide

查看:881
本文介绍了使用Glide预加载多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试预加载图片到缓存中以便以后加载它们(图片位于应用程序的资源文件夹中)

We are trying to preload images into cache memory to load them later (the images are located in the Asset folder of the application)

我们尝试了什么:

Glide.with(this)
    .load(pictureUri)
    .diskCacheStrategy(DiskCacheStrategy.ALL);

Glide.with(this)
    .load(picture_uri)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .preload();

问题:图片仅在我们尝试加载/显示时缓存:
它们必须在内存中加载才能显得更快。

The issue: Images are cached only when we are trying to load/display them: They have to be loaded in memory before so that they appear faster.

Glide.with(this)
    .load(picture_uri)
    .into(imageView);

我们还尝试使用GlideModule来增加CacheMemory大小:

We also tried to use a GlideModule to increase the CacheMemory size:

public class GlideModule implements com.bumptech.glide.module.GlideModule {
    @Override
    public void applyOptions(Context context, GlideBuilder
        builder.setMemoryCache(new LruResourceCache(100000));
    }

    @Override
    public void registerComponents(Context context, Glide glide) {
    }
}

在清单中:

 <meta-data android:name=".GlideModule" android:value="GlideModule"/>

到目前为止没有任何工作。任何想法?

Nothing is working so far. Any idea?

我们试图使用不可见的1 dp imageView,但结果是相同的:

We trying to use an invisible 1 dp imageView, but the result is the same:

for(Drawing drawing: getDrawingsForTab(tab)){

    Glide.with(this)
            .load(drawing.getImage().toUri())
            .dontAnimate()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(mPreloadCacheIv);

    for(Picture picture : getPictures()){

        Glide.with(this)
                .load(picture.getPicture().toUri())
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(mPreloadCacheIv);
    }
}


推荐答案

最好的选择是自己处理缓存,它可以让你获得更多控制权。应该很容易,因为你已经知道要加载什么位图。

The best option is to handle caching yourself, it gives you more control & should be easy as you already know what bitmaps are to be loaded.

LruCache<String, Bitmap> memCache = new LruCache<>(size) {
    @Override
    protected int sizeOf(String key, Bitmap image) {
        return image.getByteCount()/1024;
    }
};



第二:将位图加载到LruCache



Second: Load the bitmaps to the LruCache

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x; //width of screen in pixels
int height = size.y;//height of screen in pixels
Glide.with(context)
    .load(Uri.parse("file:///android_asset/imagefile"))
    .asBitmap()
    .fitCenter() //fits given dimensions maintaining ratio
    .into(new SimpleTarget(width,height) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
            memCache.put("imagefile", resource);
        }
    });



第三:使用缓存的位图



Third: Use the cached bitmaps

Bitmap image = memCache.get("imagefile");
if (image != null) {
    //Bitmap exists in cache.
    imageView.setImageBitmap(image); 
} else {
    //Bitmap not found in cache reload it 
    Glide.with(context)
         .load(Uri.parse("file:///android_asset/imagefile"))
         .into(imageView);
}

这篇关于使用Glide预加载多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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