简体   繁体   English

android中的延迟加载

[英]Lazy loading in android

I want to lazy load thumbnails of images that i receive via bluetooth in to a ListView . 我想将通过蓝牙接收的图像的缩略图延迟加载到ListView中。 I saw many implementation of LazyLoading in android, but i just could not understand the flow how the lazy loader was working. 我在Android上看到了许多LazyLoading的实现,但是我只是不明白流程中的惰性加载器是如何工作的。

Btw I referred to this site 顺便说一句,我提到这个网站

http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134 http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134

In the above implementation they use both Memory cache as well as File cache (In the class ImageLoader). 在上述实现中,它们同时使用了内存缓存和文件缓存(在ImageLoader类中)。 I can't get why is it necessary to maintain two cache? 我不明白为什么必须维护两个缓存?
And in the same class he maintains a Map imageViews. 在同一个类中,他维护一个Map imageViews。 Why would he need a mapping of imageViews and Image url? 为什么他需要imageViews和Image url的映射? .

Can anyone explain the flow of the above code or give some tips on designing my own lazyloader class. 任何人都可以解释以上代码的流程或提供一些有关设计自己的lazyloader类的提示。

Thanks in advance 提前致谢

I can't get why is it necessary to maintain two cache? 我不明白为什么必须维护两个缓存?

It is implementing a 2 level cache, so you have a first level cache in memory. 它正在实现2级缓存,因此您在内存中具有1级缓存。 This cache is faster that a disk cache(file cache) but it cannot have too much element on it. 此高速缓存比磁盘高速缓存(文件高速缓存)快,但不能包含太多元素。 The second level cache, file cache, is a bigger one but is slower. 第二级缓存(文件缓存)较大,但速度较慢。 So when it wants a image, first try to fetch it from the first level cache, if it cannot find it there, try on the second level cache. 因此,当它需要图像时,请先尝试从一级缓存中获取它,如果找不到该图像,请尝试二级缓存。 If the image is not in either, then download from a url. 如果图像均不在其中,则从URL下载。 Once the image has been retrieved from the internet, is stored on the first level cache. 从互联网上检索到图像后,该图像将存储在第一级缓存中。 If there are more image on the cache than the cache size, the least recent used image is moved from the first level cache to the second level. 如果缓存上的图像多于缓存大小,则将最近使用最少的图像从第一级缓存移到第二级。

Using a 2 level cache minimized the use of internet connection, storing those image that you have recently used. 使用2级缓存可以最大程度地减少Internet连接的使用,从而存储您最近使用的那些图像。

Why would he need a mapping of imageViews and Image url? 为什么他需要imageViews和Image url的映射?

The way the cache knows if the image you are requesting is already on the cache is looking in the map for the URL. 缓存知道您所请求的图像是否已在缓存中的方式是在地图中查找URL。 Think on it as a ID of the image. 认为它是图像的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM