2013-03-25 70 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中的知識,我不是說這只是正確的。答案可能有錯,如果有任何機構可以隨時通知我。