使用Glide预加载多个图像

An-*_*oid 31 java android image android-glide

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

我们尝试了什么:

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

Glide.with(this)
    .load(picture_uri)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .preload();
Run Code Online (Sandbox Code Playgroud)

问题:只有当我们尝试加载/显示图像时才会缓存图像:必须先将它们加载到内存中,以便它们看起来更快.

Glide.with(this)
    .load(picture_uri)
    .into(imageView);
Run Code Online (Sandbox Code Playgroud)

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

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) {
    }
}
Run Code Online (Sandbox Code Playgroud)

在清单中:

 <meta-data android:name=".GlideModule" android:value="GlideModule"/>
Run Code Online (Sandbox Code Playgroud)

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


我们尝试使用不可见的1 dp imageView,但结果是一样的:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Vin*_*hwa 24

使用以下代码缓存图像而不显示它们

  1. downloadOnly如果您要从Web下载图像并将其存储在diskCache中,请使用此方法:

    FutureTarget<File> future = Glide.with(applicationContext)
        .load(yourUrl)
        .downloadOnly(500, 500);
    
    Run Code Online (Sandbox Code Playgroud)
  2. preload如果要将它们加载到内存缓存中,请使用方法.

    Glide.with(context)
            .load(url)
            .preload(500, 500);
    
    Run Code Online (Sandbox Code Playgroud)

您可以稍后使用缓存的图像

Glide.with(yourFragment)
    .load(yourUrl)
    .into(yourView);
Run Code Online (Sandbox Code Playgroud)

  • 你应该使用`GlideApp.with(yourFragment).load(yourUrl).fitCenter().asBitmap().preload();`来修复缓存缺失。 (2认同)
  • 在我的例子中对我有帮助的是:`Glide.with(this).asDrawable().load(url).fitCenter().preload(imageView.width, imageView.height)`感谢@EmmanuelMess提供线索。 (2认同)

Sak*_*ham 18

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

第一:设置LruCache

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

第二步:将位图加载到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) {
        // the constructor SimpleTarget() without (width, height) can also be used.
        // as suggested by, An-droid in the comments
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
            memCache.put("imagefile", resource);
        }
    });
Run Code Online (Sandbox Code Playgroud)

第三:使用缓存的位图

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);
}
Run Code Online (Sandbox Code Playgroud)

  • 您的响应很好,有关信息,也可以使用新的SimpleTarget(),并指定保留原始大小的值.这就是我所做的:) (3认同)
  • 用位图淹没所有内存并不是一个好习惯,理想情况下,您不应超过分配给应用程序的内存的四分之一`(int) (Runtime.getRuntime().maxMemory() / (1024 * 4))` 如果你需要一个更大的缓存,你应该尝试不同的架构方法。 (2认同)

yoA*_*ex5 11

Glide版本4.6.1

RequestOptions requestOptions = RequestOptions
        .diskCacheStrategy(DiskCacheStrategy.ALL);

Glide.with(appContext)
        .asBitmap()
        .load(model)
        .apply(requestOptions)
        .submit();
Run Code Online (Sandbox Code Playgroud)