栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Glide预加载多个图像

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Glide预加载多个图像

最好的选择是自己处理缓存,它可以为您提供更多控制权,并且应该很容易,因为您已经知道要加载哪些位图。

首先:设置一个LruCache

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

第二:将位图加载到LruCache

Display display = getWindowManager().getDefaultDisplay();Point size = new Point();display.getSize(size);int width = size.x; //width of screen in pixelsint height = size.y;//height of screen in pixelsGlide.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);        }    });

第三:使用缓存的位图

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569283.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号