ImageView的scaleType设置不当,导致使用Glide时出现OOM
来源:互联网 发布:叮叮包车软件苹果 编辑:程序博客网 时间:2024/06/06 09:00
转载地址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0606/3002.html
来源 http://www.licheetec.com/2015/05/16/imageview-scale-type/
之前看到一篇介绍图片加载库——Glide的文章。
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0327/2650.html
看到连Google官方也在用。自动缓存,自动图片压缩,支持加载网络图片、加载Gif等等,支持集成Volley和okHttp,总之好处多多。
于是找到 Glide 在Github上面的项目主页,按照说明写了个简单的Demo。
简单描述一下Demo吧,就是在tomcat上面弄一个webapp,里面就放了70来张非常大的jpg图片(某个エロゲーム 的CG,因为内容比较和谐,我就不细说了,总之每张图都有2560*1400
分辨率,sample,刚好用来测试性能),还有包含着这些图片地址的json文件。
然后就是写个ListView,加载这些图片了。什么convertView复用,ViewHolder也弄好了。
运行,图片顺利加载,慢慢拖动,貌似也没问题,但是一快速滑动(也就是fling),就变得非常卡,Logcat出现 OutOfMemoryError。
检查了下程序逻辑,没啥问题,Glide的介绍提到了会按照ImageView大小了缩放和缓存图像,不至于这样啊。
百思不得其解,就去Glide项目页那里,用蹩脚的英文发了个issue,然后@TWiStErRob大神也很用心地回答了。
https://github.com/bumptech/glide/issues/464
Glad it helped. Feel free to ask further questions if you’re stuck.
For posterity: fitXY forces Glide to read at full resolution and then let the ImageView handle the distortion (fitXY doesn’t keep aspect ratio). If you want to keep distorting the images, you can keep fitXY on the imageView and call centerCrop() or fitCenter() before load(): this will make Glide downsample the image (less memory) before handing it to ImageView for distortion.
Here’s a further tip I just noticed: you could also change android:src to tools:src. That way you will save some layout passes (ImageView does a layout on every setImage*() call) and you can skip loading a bitmap (launcher icon when inflating each item) unnecessarily (you’ll overwrite it anyway with Glide). The tools attributes are not compiled into the apk, so it’s optimal to use them in these situations.
原来是ImageView的scaleType
的问题,当设置为fitXY
时,虽然ImageView显示那么点尺寸,但是,但是Glide加载图片时,却是以全分辨率加载的,于是加载几张,就OOM了。
改成fitCenter
或者centerCrop
(试了一下fitStart
、fitEnd
也行,总之看需求了),就好了,会自动缓存小图,滚动起来也非常流畅。
完全没想到原来坑在scaleType
上面,这下记住了。
最后感谢@TWiStErRob大神的耐心回答,Github已follow。
- ImageView的scaleType设置不当,导致使用Glide时出现OOM
- ImageView的scaleType设置不当,导致使用Glide时出现OOM
- ImageView的scaleType设置不当,导致使用Glide时出现OOM
- ImageView的scaleType设置不当,导致使用Glide时出现卡顿
- recycleview 中 glide ImageView的scaleType设置不当,滑动卡顿
- ImageView.ScaleType的使用
- ImageView ScaleType的使用
- 使用Glide导致内存泄漏最终OOM的问题解决
- ImageView的ScaleType属性设置
- ResultSet参数设置不当导致OOM
- android中ImageView属性scaletype的设置
- Android中ImageView的ScaleType设置
- 一个关于 imageView 设置 scaleType 的问题
- ImageView的scaleType属性设置区别
- ImageView.ScaleType设置图解
- ImageView.ScaleType设置图解
- ImageView.ScaleType设置图解
- ImageView.ScaleType设置图解
- POJ之路10--1125 Stockbroker Grapevine
- mongodb学习
- 关于Android四大组件之一broadcast receiver的详解(广播接收者)
- c++基础学习(05AM)
- 超级表哥终于出柜了
- ImageView的scaleType设置不当,导致使用Glide时出现OOM
- 写在入职3个月
- win32 简单画图
- ucore操作系统lab2实验报告
- hadoop任务提交过程
- Android 坐标常识
- 像 geek 一样写博客
- 设计模式-创建型模式#建造者模式Builder
- 有关munmap_chunk() invalid pointer错误