2015-10-20 91 views
0

我有一個ListView和一個從API加載各種數據的適配器。其中一個數據是在線圖像的URL。我下載圖像並將其設置在適配器中的ImageView中。在ListView中緩存下載的圖像

問題是,當我在ListView中上下滾動時,圖像被重新下載,並且在這樣做時存在明顯的滯後。此外,在瞬間,它錯誤地顯示錯誤的圖像。

我需要以某種方式緩存這些圖像只下載一次,並在該ListView中正確設置它們 - 我該怎麼做?

編輯: 我使用通用的圖像加載器加載我的圖片是這樣的:

ImageLoader imageLoader = ImageLoader.getInstance(); 
imageLoader.displayImage(imageUrl, viewHolder.thumbnailImageView); 

但它似乎並沒有對它們進行緩存......我必須做些什麼額外的?

+0

使用圖像庫,如壁畫加載和緩存畫面,供各位 – Breavyn

回答

1

使用ImageLoaders

Glide

ImageView的ImageView的=(ImageView的)findViewById(R.id.my_image_view);

Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView); 

簡單圖像加載。檢查您可以與聽衆一起使用它們的鏈接。

你還可以嘗試:

picasso

Android-Universal-Image-Loader

編輯:作爲OP說,他使用UIL。

]您應該使用圖像感知。 你可以擁有自己的高緩存內存配置。

public void initializeImageLoader(final Context sContext) { 
     final ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(sContext) 
       .memoryCacheExtraOptions(480, 800) // default = device screen dimensions 
       .taskExecutor(AsyncTask.THREAD_POOL_EXECUTOR) 
       .taskExecutorForCachedImages(AsyncTask.THREAD_POOL_EXECUTOR) 
       .threadPoolSize(3) // default 
       .threadPriority(Thread.NORM_PRIORITY - 1) // default 
       .tasksProcessingOrder(QueueProcessingType.FIFO) // default 
       .denyCacheImageMultipleSizesInMemory() 
       .memoryCache(new UsingFreqLimitedMemoryCache(50 * 1024 * 1024)) // default 
       .memoryCacheSize(50 * 1024 * 1024) 
       .imageDownloader(new BaseImageDownloader(sContext)) // default 
       .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default 
       .build(); 
     ImageLoader.getInstance().init(config); 
    } 

在獲取實例之前使用此初始化。

initializeImageLoader(context); ImageLoader imageLoader = ImageLoader.getInstance();

設置裝載機的選項。

final DisplayImageOptions options = new DisplayImageOptions.Builder() 
       .showImageOnLoading(R.drawable.loading) 
       .showImageForEmptyUri(R.drawable.noimage) 
       .showImageOnFail(R.drawable.noimage) 
       .resetViewBeforeLoading(false) //default 
       .bitmapConfig(Bitmap.Config.RGB_565) 
       .cacheInMemory(true) 
       .cacheOnDisk(false) //default 
       .build(); 

並將其設置爲您的裝載機:

final ImageAware imageAware = new ImageViewAware(viewHolder.thumbnailImageView, false); 
imageLoader.displayImage(imageUrl, imageAware, options); 
+0

我用的是通用的圖像加載器加載我的圖像,但它不似乎緩存他們......我該怎麼做..? – Micro

+1

發佈代碼。 – Dhina