2013-03-25 72 views

回答

37

懒惰列表延迟从sd卡或从服务器使用url加载图像。这就像按需加载图像。

图像可以缓存到本地SD卡或手机内存。网址被认为是关键。如果密钥存在于SD卡中,则显示来自SD卡的图像,通过从服务器下载并显示图像并将其缓存到您选择的位置。缓存限制可以设置。您也可以选择自己的位置来缓存图像。缓存也可以被清除。

而不是用户等待下载大图像,然后显示懒惰列表,按需加载图像。由于图像缓存,您可以离线显示图像。

https://github.com/thest1/LazyList。懒列表

在你getview

imageLoader.DisplayImage(imageurl, imageview); 

ImageLoader的显示方式

public void DisplayImage(String url, ImageView imageView) //url and imageview as parameters 
    { 
    imageViews.put(imageView, url); 
    Bitmap bitmap=memoryCache.get(url); //get image from cache using url as key 
    if(bitmap!=null)   //if image exists 
     imageView.setImageBitmap(bitmap); //dispaly iamge 
    else //downlaod image and dispaly. add to cache. 
    { 
     queuePhoto(url, imageView); 
     imageView.setImageResource(stub_id); 
    } 
    } 

懒列表的替代方案是通用图像装载机

https://github.com/nostra13/Android-Universal-Image-Loader。它基于Lazy List(基于相同原理)。但它有很多其他配置。我宁愿使用**** Universal Image Loader ****因为它给你更多的配置选项。如果downlaod失败,您可以显示错误图像。可以显示圆角的图像。可以缓存在光盘或内存上。可以压缩图像。

在您的自定义适配器构造

File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder"); 

// Get singletone instance of ImageLoader 
imageLoader = ImageLoader.getInstance(); 
// Create configuration for ImageLoader (all options are optional) 
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a) 
      // You can pass your own memory cache implementation 
     .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation 
     .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) 
     .enableLogging() 
     .build(); 
// Initialize ImageLoader with created configuration. Do it once. 
imageLoader.init(config); 
options = new DisplayImageOptions.Builder() 
.showStubImage(R.drawable.stub_id)//display stub image 
.cacheInMemory() 
.cacheOnDisc() 
.displayer(new RoundedBitmapDisplayer(20)) 
.build(); 

在你getView()

ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
    imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options 

你可以用其他选项配置,以满足您的需求。

随着延迟加载/通用图像加载器,您可以查看持有人的平滑滚动和性能。 http://developer.android.com/training/improving-layouts/smooth-scrolling.html

+0

嘿raghu,我无法从当地的uri中获取图片吗?请帮助我吗?http://stackoverflow.com/questions/20109034/lazy-list-fetching-images-from-sdcard-local-url – 2013-11-21 19:12:59

+0

很棒解释,所以懒惰列表类似于毕加索或滑翔? – Fshamri 2015-08-07 14:10:58

+1

@ user2198400这些是图像缓存库。是的,他们帮助延迟加载。也请阅读关于listview回收机制。 – Raghunandan 2015-08-07 14:12:34

5

我认为这是相反的方式。 AFAIK,Lazy Loading是定义,只在需要时才加载数据,这是一个很好的设计实践。

所以我相信同样适用于此,只是这次它指的是列表视图。

如果我错了,请纠正我。

2

懒惰列表的最佳示例是Facebook通知,消息,请求。当你滚动时,数据将被加载。

9

AFAIK,我将用示例向您解释 如果列表中包含许多带有文本的图像,则需要一段时间才能加载列表,因为您需要下载图像,并且需要将它们填充到列表中。假设您的列表包含100个图像下载每个图像并将其显示为列表项需要很长时间。让用户等待,直到加载的图像不是用户友好的。 所以我们需要做的。在这个时间点懒惰名单进入图片。这是一个想法,让图像在后台加载并显示文本的意思。

大家都知道,listview会为每个视图回收其视图。即如果您的列表视图包含40个元素,则listview将不会为40个项目分配内存,而是为可见项目分配内存,即,一次只能看到10个项目。所以listview会分配10个项目memory。

所以,当你滚动视图,然后视图将刷新。因为你会失去你对图像的引用,你需要下载它们。为了避免这种情况,高速缓存进入画面。

这个例子是基于我在listview中的知识,我不是说这只是正确的。答案可能有错,如果有任何机构可以随时通知我。