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
使用以下代码缓存图像而不显示它们
downloadOnly如果您要从Web下载图像并将其存储在diskCache中,请使用此方法:
FutureTarget<File> future = Glide.with(applicationContext)
    .load(yourUrl)
    .downloadOnly(500, 500);
Run Code Online (Sandbox Code Playgroud)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)
Sak*_*ham 18
最好的选择是自己处理缓存,它可以让你更容易控制并且应该很容易,因为你已经知道要加载什么位图.
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)
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)
        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)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           22645 次  |  
        
|   最近记录:  |