位图中的适配器异步下载,重点是Bitmap.recycle() [英] Asynchronous download of Bitmaps in an Adapter, with emphasis on Bitmap.recycle()

查看:151
本文介绍了位图中的适配器异步下载,重点是Bitmap.recycle()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我如何成为一个好机制,异步。图像的使用在ListView / GridView的下载? 有许多建议,但每次只考虑的典型要求的一小部分。

Could someone tell me how to make a good mechanism for async. download of images for use in a ListView/GridView? There are many suggestions, but each only considers a small subset of the typical requirements.

下面我列出了一些合理的因素(需求或事物来考虑),我和我的collegues,是无法满足的一次。
我不要求为code(尽管它会受到欢迎),就像描述管理该位图的方法。

Below I've listed some reasonable factors (requirements or things to take into account) that I, and my collegues, are unable to satisfy at once.
I am not asking for code (though it would be welcome), just an approach that manages the Bitmaps as described.

  1. 在下载或位图
  2. 无重复
  3. 取消下载/分配,将不再需要,或有可能被自动删除(SoftReference等)
  4. 图片
  5. 注:一个适配器可以有多个视图为相同的ID(调用getView(0)非常频繁)
  6. 注:也不能保证一个视图不会丢失,而不是回收(考虑列表/ GridView控件调整大小或过滤通过的文本)
  7. 的意见和数据的分离/逻辑(尽可能)
  8. 在不启动在每次下载一个单独的线程(UI可见放缓)。使用队列/堆栈(的BlockingQueue?)和线程池,或诸如此类....但要结束,如果活动已停止。
  9. 净化足够的位图从列表/网格,$ P $的当前位置较远pferably只有当内存被需要
  10. 电话上的每个位图是要被丢弃的再循环()。
  11. 注:外部存储器可能不可用(在所有或所有的时间),并且,如果使用的话,应清除(只是在这里下载的图像)的ASAP(考虑活动破坏/休闲搭载Android)
  12. 注:数据是可以改变的:删除条目(多选和放大器;删除),并添加(在后台线程)。已下载的位图应保持,只要输入他们联系依然存在。
  13. setTextFilterEnabled(真)(如果基于ArrayAdapter的机制,会影响到数组索引)
  14. 能用于ExpandableList(影响到缩略图显示的顺序)
  15. (可选)下载一个位图时,只刷新相关ImageView的(列表中的项目可能会很复杂)
  1. No duplication of downloaders or Bitmaps
  2. Canceling downloads/assigning of images that would no longer be needed, or are likely to be automatically removed (SoftReference, etc)
  3. Note: an adapter can have multiple Views for the same ID (calls to getView(0) are very frequent)
  4. Note: there is no guarantee that a view will not be lost instead of recycled (consider List/GridView resizing or filtering by text)
  5. A separation of views and data/logic (as much as possible)
  6. Not starting a separate Thread for each download (visible slowdown of UI). Use a queue/stack (BlockingQueue?) and thread pool, or somesuch.... but need to end that if the Activity is stopped.
  7. Purging Bitmaps sufficiently distant from the current position in the list/grid, preferably only when memory is needed
  8. Calling recycle() on every Bitmap that is to be discarded.
  9. Note: External memory may not be available (at all or all the time), and, if used, should be cleared (of only the images downloaded here) asap (consider Activity destruction/recreation by Android)
  10. Note: Data can be changed: entries removed (multi-selection & delete) and added (in a background Thread). Already downloaded Bitmaps should be kept, as long as the entries they're linked to still exist.
  11. setTextFilterEnabled(true) (if based on ArrayAdapter's mechanism, will affect array indexes)
  12. Usable in ExpandableList (affects the order the thumbnails are shown in)
  13. (optional) when a Bitmap is downloaded, refresh ONLY the relevant ImageView (the list items may be very complex)

请不要发布答案个别点。我的问题是,越是集中在一些方面,模糊的人成为,海森堡般
每增加了难度,尤其是Bitmap.recycle,这就需要在运行过程中的破坏活动被称为的尺寸(注意的onDestroy,甚至可能的onStop不叫)。
这也precludes <一href="http://ballardhack.word$p$pss.com/2010/04/10/loading-images-over-http-on-a-separate-thread-on-android/">relying在SoftReferences 的。照片 有必要,或者我甚至之后的任何数量的GC,睡眠得到的OutOfMemoryError (20多岁,甚至),产量和在一个try-catch巨大的数组分配归零位图后(强制控制内存不足)。
我是<一个href="http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize">resampling位图了。

Please do not post answers for individual points. My problem is that that the more we focus on some aspects, the fuzzier others become, Heisenberg-like.
Each adds a dimension of difficulty, especially Bitmap.recycle, which needs to be called during operation and on Activity destruction (note that onDestroy, even onStop might not be called).
This also precludes relying on SoftReferences.
It is necessary, or I get OutOfMemoryError even after any number of gc, sleep (20s, even), yield and huge array allocations in a try-catch (to force a controlled OutOfMemory) after nulling a Bitmap.
I am resampling the Bitmaps already.

推荐答案

在最后,我选择了完全无视回收问题。它只是增加了一层对管理过程的顶部是不可能的难度。
如果没有这种负担(只是让适配器等停止显示图像),我用了一个经理地图&LT;字符串,SoftReference&LT;位图&GT;&GT; 来存储下的URL下载的位图。
此外,2-4 AsyncTasks(利用两个doInBackground和onProgressUpdate的;停止通过添加抛出InterruptedException特殊工种)从 LinkedBlockingDeque&LT采取的工作; WeakReference的&LT; D​​ownloadingJob&GT;&GT; 支持由的WeakHashMap&LT;对象,设置&LT; D​​ownloadingJob&GT;&GT;
双端队列(LinkedBlockingDeque code复制上较早使用API​​)是一个队列,作业可以离开,如果他们不再需要。该映射作业创作者键,因此,如果一个适配器需要下载,然后被去除,它是从地图中删除,并且,作为结果,所有的作业从队列中消失。

作业会,如果图像已经present,返回同步。它也可以包含的数据可以在它涉及一个AdapterView确定哪些位置的包。

缓存也做了SD卡上,的(如果可用)的,在URLEn codeD的名字。 (清洁部分,从最老的,关于应用程序的启动,和/或使用deleteOnExit()
要求包括:如果修改 - 因为如果我们有一个缓存的版本,检查更新。照片
同样的事情,也可以用于XML解析,和大多数其他的数据采集。
如果我清理阶级了,我会发布了code。

In the end, I chose to disregard the recycling bug entirely. it just adds a layer of impossible difficulty on top of a manageable process.
Without that burden (just making adapters, etc stop showing images), I made a manager using Map<String, SoftReference<Bitmap>> to store the downloaded Bitmaps under URLs.
Also, 2-4 AsyncTasks (making use of both doInBackground and onProgressUpdate; stopped by adding special jobs that throw InterruptedException) taking jobs from a LinkedBlockingDeque<WeakReference<DownloadingJob>> supported by a WeakHashMap<Object, Set<DownloadingJob>>.
The deque (LinkedBlockingDeque code copied for use on earlier API) is a queue where jobs can leave if they're no longer needed. The map has job creators as keys, so, if an Adapter demands downloads and then is removed, it is removed from the map, and, as a consequence, all its jobs disappear from the queue.

A job will, if the image is already present, return synchronously. it can also contain a Bundle of data that can identify which position in an AdapterView it concerns.

Caching is also done on an SD card, if available, under URLEncoded names. (cleaned partially, starting with oldest, on app start, and/or using deleteOnExit()
requests include "If-Modified-Since" if we have a cached version, to check for updates.

The same thing can also be used for XML parsing, and most other data acquisition.
If I ever clean that class up, I'll post the code.

这篇关于位图中的适配器异步下载,重点是Bitmap.recycle()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆