一、介绍
Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。所以,如果你的程序里需要这个功能的话,那么不妨试试它。因为已经封装好了一些类和方法。我们 可以直接拿来用了。而不用重复去写了。其实,写一个这方面的程序还是比较麻烦的,要考虑多线程,缓存,内存溢出等很多方面。但是,你也可以参考这个例子来自己写出更好的程序。在此为大家介绍一下:
二、特点
多线程的图像加载
的可能性的宽调谐对ImageLoader的配置(线程池的大小,HTTP选项,内存和光盘高速缓存,显示图像,以及其他)
的图像的可能性中的缓存存储器和/或设备的文件器系统(或SD卡)
可以“听”加载过程中
可自定义每个显示的图像调用分隔的选项
Widget支持
Android 1.5以上支持
三、使用方法
这是 一个开源的Android关于下载显示图片的工具类,在这个下载包里面偶jar文件,用于我们导入项目使用,具体使用方法在包里面也含有。下面是一个例子:
ImageView imageView = ...
String imageUrl = "http://site.com/image.png"; // or "file:///mnt/sdcard/images/image.jpg"
ProgressBar spinner = ...
File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "UniversalImageLoader/Cache");
// Get singletone instance of ImageLoader
ImageLoader imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional, use only those you really want to customize)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.memoryCacheExtraOptions(480, 800) // max width, max height
.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75) // Can slow ImageLoader, use it carefully (Better don't use it)
.threadPoolSize(3)
.threadPriority(Thread.NORM_PRIORITY - 1)
.denyCacheImageMultipleSizesInMemory()
.offOutOfMemoryHandling()
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(new URLConnectionImageDownloader(5 * 1000, 20 * 1000)) // connectTimeout (5 s), readTimeout (20 s)
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.enableLogging()
.build();
// Initialize ImageLoader with created configuration. Do it once on Application start.
imageLoader.init(config);
// Creates display image options for custom display task (all options are optional)
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.cacheInMemory()
.cacheOnDisc()
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.displayer(new RoundedBitmapDisplayer(20))
.build();
// Load and display image
imageLoader.displayImage(imageUrl, imageView, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted() {
spinner.show();
}
@Override
public void onLoadingFailed(FailReason failReason) {
spinner.hide();
}
@Override
public void onLoadingComplete(Bitmap loadedImage) {
spinner.hide();
}
@Override
public void onLoadingCancelled() {
// Do nothing
}
});
// Just load image
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory()
.cacheOnDisc()
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.displayer(new FakeBitmapDisplayer())
.build();
ImageSize minImageSize = new ImageSize(120, 80);
imageLoader.loadImage(context, imageUrl, minImageSize, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(Bitmap loadedImage) {
// Do whatever you want with loaded Bitmap
}
});
四、使用信息
缓存不是默认启用。如果你想将被缓存在内存中加载图像和/或盘那么你应该启用缓存displayimageoptions方式如下:
// Create default options which will be used for every
// displayImage(...) call if no options will be passed to this method
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
...
.cacheInMemory()
.cacheOnDisc()
...
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
...
.defaultDisplayImageOptions(defaultOptions)
...
.build();
ImageLoader.getInstance().init(config); // Do it on Application start
// Then later, when you want to display image
ImageLoader.getInstance().displayImage(imageUrl, imageView); // Default options will be used
分享到:
相关推荐
Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置),易懂易用。
Android-Universal-Image-Loader使用详解
Android-Universal-Image-Loader的示例。 效果 http://blog.csdn.net/wwj_748/article/details/10079311
图片缓存框架Android-Universal-Image-Loader
Android-Universal-Image-Loader jar包
Android-Universal-Image-Loader官方的Demo和jar包,原Demo图片URL不能访问,已经被我替换成可以访问的URL
Android-Universal-Image-Loader开源项目的简要说明及使用实例
来自俄罗斯的异步图片加载缓存 Android-Universal-Image-Loader 图片缓存Android-Universal-Image-Loader-master
Android-Universal-Image-Loader-1.9.5,安卓图片加载框架
图片异步加载图片缓存,增加图片缓存的效率,提高开发速度。
Android UniversalImageLoader 异步加载图片
Android-Universal-Image-Loader-maste 移动开发.zip