Rag*_*dan 37
懒惰列表是从SD卡或从服务器使用URL延迟加载图像.这就像点播加载图片一样.
图像可以缓存到本地SD卡或手机内存.网址被认为是关键.如果密钥存在于SD卡中,则显示来自SD卡的图像通过从服务器下载显示图像并将其缓存到您选择的位置.可以设置缓存限制.您还可以选择自己的位置来缓存图像.缓存也可以被清除.
而不是用户等待下载大图像然后显示惰性列表,而是按需加载图像.由于图像是缓存的,因此您可以离线显示图像.
https://github.com/thest1/LazyList.懒惰清单
在你的getview中
imageLoader.DisplayImage(imageurl, imageview);
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Lazy List的替代方案是Universal Image Loader
https://github.com/nostra13/Android-Universal-Image-Loader.它基于Lazy List(基于相同的原理).但它有很多其他配置.我更喜欢使用****通用图像加载器****因为它为您提供了更多的配置选项.如果下载失败,您可以显示错误图像.可以显示带圆角的图像.可以缓存在光盘或内存上.可以压缩图像.
在自定义适配器构造函数中
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();
Run Code Online (Sandbox Code Playgroud)
在你的getView()中
ImageView image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options
Run Code Online (Sandbox Code Playgroud)
您可以配置其他选项以满足您的需求.
与延迟加载/通用图像加载器一起,您可以查看支架以实现平滑滚动和性能. http://developer.android.com/training/improving-layouts/smooth-scrolling.html.
AFAIK,我将用示例向您解释如果您列出包含大量带有Text的图像,则需要一些时间来加载列表,因为您需要下载图像,并且需要在列表中填充它们.假设您的列表包含100个图像下载每个图像并将其显示为listitem将花费大量时间.使用户等到图像加载不是用户友好的.所以我们需要做什么.在这个时间点懒惰列表进入图片.这是让图像在背景中加载并显示文本的意思.
每个人都知道listview为每个视图回收其视图.即如果你的列表视图包含40个elemtns,那么listview将不会为40个项目分配内存,而是为可见项目分配内存,即说你一次只能看到10个项目.所以listview将分配10个项目meemory.
因此,当您滚动视图时,视图将刷新.因为你将丢失对图像的引用,你需要下载它们.为了避免这种情况,缓存进入了画面.
这个例子基于我在listview中的知识,我不是说这只是正确的.答案可能有问题,如果有任何身体发现可以随时通知我.