原理
OnMeasure
常用优化
convertview
ViewHolder
减少findviewbyid的时间,viewHolder也是会被重用的
高级优化
仅当GridView静止时才去下载图片,GridView滑动时取消所有正在下载的任务
下载的任务应该由onScrollStateChanged里调用,但首次进入程序时onScrollStateChanged并不会调用
问题
http://www.cnblogs.com/lesliefang/p/3619223.html提到重用了 convertView 且有异步操作会导致图片错位
解决方案,加tag,他的方法在ImageView加一个Tag,
我的方法在ViewHolder里加一个id
问题
同一个 URL 请求的重复发送,退出 activity 后队列中请求的 cancel
问题
现LRUCache并不不会释放内存,因为LRUCache的entryRemoved方法默认不会对bitmap做recycle操作,只是在cache里没有索引了,bitmap还留存在内存中,而Imageview还持有对bitmap的引用,因此GC不会回收它,内存并没释放。而你的GridView中内存之所以没有增加,是因为GridView会重用之前的ImageView, 而ImageView重新setImageBitmap, 就释放掉了对之前bitmap的引用,之前的bitmap就会被回收,因此能保持内存的不变,再多的图片也不会挂
问题
http://stackoverflow.com/questions/10743381/when-should-i-recycle-a-bitmap-using-lrucache
When you are done with a Bitmap
(e.g., row in a ListView
is recycled), you check to see if it is still in the cache. If it is, you leave it alone. If it is not, you recycle()
it