這個也是近期項目中使用到的第三方圖片載入框架。在這裏也本身總結一下,簡單的介紹一些使用的方式。java
1.多線程下載圖片。圖片可以來源於網絡,文件系統,項目文件夾assets中以及drawable中等 2.支持任意的配置ImageLoader,好比線程池。圖片下載器,內存緩存策略,硬盤緩存策略,圖片顯示選項以及其它的一些配置 3.支持圖片的內存緩存,文件系統緩存或者SD卡緩存 4.支持圖片下載過程的監聽 5.依據控件(ImageView)的大小對Bitmap進行裁剪,下降Bitmap佔用過多的內存 6.較好的控制圖片的載入過程,好比暫停圖片載入。又一次開始載入圖片,通常使用在ListView,GridView中。滑動過程當中暫停載入圖片。中止滑動的時候去載入圖片 7.提供在較慢的網絡下對圖片進行載入
ImageLoader Jar包引入項目中:https://github.com/nostra13/Android-Universal-Image-Loader/raw/master/downloads/universal-image-loader-1.9.5.jar
或者是下載這個項目,而後導入到project中。使用庫依賴的方式進行引用,假設還不太懂怎麼導入demo和庫依賴,可以看下
AndroidStudio導入本地和github項目,以及怎麼加入第三方依賴介紹git
配置ImageLoder參數(ImageLoaderConfiguration) ImageLoaderConfiguration configuration = ImageLoaderConfiguration .createDefault(this);
初始化ImageLoader ImageLoader.getInstance()
displayImage(), loadImage(),loadImageSync()
好了,咱們開始載入圖片吧。
這個時候,咱們需要配置imageloader的參數。也就是在application裏面配置。這裏咱們的application使用的是單例模式:github
public class MyApplication extends Application{
private static MyApplication instance=null;
@Override
public void onCreate() {
super.onCreate();
this.instance=this;
initImageLoader(getApplicationContext());
}
public static MyApplication getInstance(){
return instance;
}
private void initImageLoader(Context context){
//cacheDir這裏是獲取到他默認的本地緩存文件夾。這StorageUtils是他這個imageloader裏面的工具類,默認的緩存文件夾是包名/cache文件夾下(固然本身可以改變)
File cacheDir = StorageUtils.getCacheDirectory(context);
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration
.Builder(context)
.threadPoolSize(5)//線程池
.diskCache(new UnlimitedDiskCache(cacheDir))//內存卡
.threadPriority(Thread.NORM_PRIORITY -2)//線程優先級
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LargestLimitedMemoryCache(2 * 1024 * 1024))//內存緩存
.memoryCacheSize(2 * 1024 * 1024)//內存緩存大小
.diskCacheSize(50 * 1024 * 1024)//存儲卡緩存大小
.diskCacheFileCount(100)//存儲卡文件個數
.memoryCacheSizePercentage(13) // default
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.imageDownloader(new BaseImageDownloader(this, 5 * 1000, 30 * 1000)) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
//.writeDebugLogs()
.tasksProcessingOrder(QueueProcessingType.FIFO) //先進先出
.build();
ImageLoader.getInstance().init(configuration);
}
}
接下來,咱們就要獲取imageloader的實例。和設置DisplayImageOptions的參數,這裏我附上一張DisplayImageOptions配置圖:緩存
mImageLoader = ImageLoader.getInstance();
mOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher)//圖片載入的時候顯示的默認圖
.showImageForEmptyUri(R.mipmap.ic_launcher)//圖片的地址爲空的時候顯示的圖
.showImageOnFail(R.mipmap.ic_launcher)//圖片載入失敗的時候顯示
.cacheOnDisk(true) //設置保存在sdcard中
.cacheInMemory(true) //設置保存在內存其中
.build();
最後咱們就要載入圖片了:
載入以前:markdown
載入成功後:
是否是很是easy呢。而且配置也是通俗易懂的。只是不幸的是,這個框架。已經中止了更新,只是我相信,這麼優秀的開源框架,仍是會有很是多人記着的。網絡
固然了哈,個人項目中用到的也就是載入圖片,並沒實用到其它的厲害的方法,比方多線程
// Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view
// which implements ImageAware interface)
imageLoader.displayImage(imageUri, imageView, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
//開始載入
...
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
//載入失敗
...
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//載入完畢
...
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
//載入取消
...
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
//載入百分比
...
}
});
// Load image, decode it to Bitmap and return Bitmap to callback
ImageSize targetSize = new ImageSize(80, 50); // result Bitmap will be fit to this size
imageLoader.loadImage(imageUri, targetSize, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
}
});
// Load image, decode it to Bitmap and return Bitmap synchronously
ImageSize targetSize = new ImageSize(80, 50); // result Bitmap will be fit to this size
Bitmap bmp = imageLoader.loadImageSync(imageUri, targetSize, options);
好了,想要了解不少其它的有關Universal-Image-Loader,可以去官網下載下來慢慢研究,這裏就很少作解釋了哈。app