◆ImageLoader
概述
UIL旨在為圖像加載,緩存和顯示提供強大,靈活和高度可定制的工具。 它提供了大量的配置選項和良好的控制圖像加載和緩存過程。
特性:
☆ 多線程圖像加載(異步或同步);
☆ ImageLoader配置的廣泛定制(線程執行器,下載器,解碼器,內存和磁盤緩存,顯示圖像選項等)。
☆ 每個顯示圖像調用的許多自定義選項(存根圖像,緩存開關,解碼選項,位圖處理和顯示等);
☆ 內存和/或磁盤上的圖像緩存(設備的文件系統或SD卡);
☆ 監聽加載過程(包括下載進度);
支持URI:
"http://site.com/image.png" // from Web
"file:///mnt/sdcard/image.png" // from SD card
"file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail)
"content://media/external/images/media/13" // from content provider
"content://media/external/video/media/13" // from content provider (video thumbnail)
"assets://image.png" // from assets
"drawable://" + R.drawable.img // from drawables (non-9patch images)
代碼部分
項目中使用Universal-Image-Loader:
S1.導入jar包或添加依賴;
universal-image-loader-1.9.5.jar
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
S2.添加訪問權限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
S3.初始化/配置:
ImageLoaderConfiguration configuration = ImageLoaderConfiguration
.createDefault(this);
ImageLoader.getInstance().init(configuration);
主要方法:
// 配置/初始化
File cacheDir = StorageUtils.getCacheDirectory(this);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions 緩存最大圖片大小
.diskCacheExtraOptions(480, 800, null) // 閃存最大圖片大小
.threadPoolSize(3) // default 最大線程數
.threadPriority(Thread.NORM_PRIORITY - 2) // default 線程優先級
.tasksProcessingOrder(QueueProcessingType.FIFO) // default 線程處理隊列,先進先出
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024)) // LruMemory
.memoryCacheSize(2 * 1024 * 1024) // 緩存
.memoryCacheSizePercentage(13) // default 緩存比例?
.diskCache(new UnlimitedDiskCache(cacheDir)) // default 閃存緩存
.diskCacheSize(50 * 1024 * 1024) // 閃存緩存大小
.diskCacheFileCount(100) // 閃存緩存圖片文件數量
// .diskCacheFileNameGenerator(new Md5FileNameGenerator())
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default 文件名
.imageDownloader(new BaseImageDownloader(this)) // default
.imageDecoder(new BaseImageDecoder(true)) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs() // LOG
.build();
ImageLoader.getInstance().init(config);
// 加載圖片
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub) // resource or drawable
.showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable
.showImageOnFail(R.drawable.ic_error) // resource or drawable
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(1000)
.cacheInMemory(false) // default
.cacheOnDisk(false) // default
.preProcessor(...)
.postProcessor(...)
.extraForDownloader(...)
.considerExifParams(false) // default
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
.bitmapConfig(Bitmap.Config.ARGB_8888) // default
.decodingOptions(...)
.displayer(new SimpleBitmapDisplayer()) // default
.handler(new Handler()) // default
.build();
ImageLoader.getInstance().displayImage(imageUrl, imageView);
ImageLoader.getInstance().displayImage(imageUrl, imageView,options);
ImageLoader.getInstance().displayImage(imageUrl, imageView, options, listener);
new ImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {
}
@Override
public void onLoadingFailed(String s, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String s, View view, Bitmap bitmap) {
}
@Override
public void onLoadingCancelled(String s, View view) {
}
})
參考:
nostra13/Android-Universal-Image-Loader github
博客
