一、介绍
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的例子程序:
- MediaHouse, UPnP/DLNA Browser
- Деловой Киров
- Бизнес-завтрак
- Menu55
- SpokenPic
- Kumir
- EUKO 2012
- TuuSo Image Search
- Газета Стройка
- Prezzi Benzina (AndroidFuel)
- Quiz Guess The Guy
- Volksempfänger (alpha)
- ROM Toolbox Lite | Pro
- London 2012 Games
- 카톡 이미지 - 예쁜 프로필 이미지
- dailyPen
- TK App
- Mania!
- Stadium Astro
- Chef Astro
- Lafemme Fashion Finder
- FastPaleo
- Live Soccer Scores
- friendizer
- LowPrice lowest book price
- bluebee
- Game PromoBox
- EyeEm - Photo Filter Camera
- Festival Wallpaper
- Gaudi Hall
- Spocal
- PhotoDownloader for Facebook
大家 可以好好看看这些例子,他们是如何把这个组件应用到程序里面的。这个开源程序的下载地址是:https://nodeload.github.com/nostra13/Android-Universal-Image-Loader/zipball/master