ImageLoader作用 AAAA
https://github.com/nostra13/Android-Universal-Image-Loader
ImageLoader作用
1.多線程下載圖片,圖片可以來源於網絡,文件系統,項目文件夾assets中以及drawable中
2.支持圖片的內存緩存,文件系統緩存或者SD卡緩存
3.支持圖片下載過程的監聽
4.根據控件(ImageView)的大小對Bitmap進行裁剪,減少Bitmap占用過多的內存
加載第三方庫
Gradle代碼
‘com.nostra13.universalimageloader:universal-image-loader:1.9.4’
權限
1
|
<uses-permission android:name="android.permission.INTERNET" />
|
ImageLoader加載的類型
1
|
"http://site.com/image.png"// from Web
|
ImageLoader的加載、顯示配置:
加載Image時的配置(ImageLoaderConfiguration)
自定義配置 - 通過new ImageLoaderConfiguration.Builder().builder()方法進行實例化。
默認配置 - 通過ImageLoaderConfiguration 的createDefault進行實例化。
- 默認配置
1
|
File diskCache = StorageUtils.getOwnCacheDirectory(context, "BNJ_IMAGE_CACHE/");
|
使用
通過ImageLoader的getInstance().init()方法傳入上述options對象.
顯示Image時的配置(DisplayImageOptions)
自定義配置 - 通過new DisplayImageOptions.Builder().builder()方法實例化對象
1
|
DisplayImageOptions options = new DisplayImageOptions.Builder()
|
加載圖片的方法
loadImage跟displayImage
A.ImageLoader.getInstance().loadImage(uri地址,圖片大小,上述配置,監聽器,進度監聽器);
B.ImageLoader.getinstance().displayImage(uri地址,控件,上述配置,監聽器,進度監聽器);
區別
A方法可以設置圖片大小,即自定義下載圖片的大小
B方法會根據控件大小及ImageScaleType來裁剪圖片
常用displayImage方法
監聽器有兩種
SimpleImageLoadingListener(簡單的監聽器)
ImageLoadingListener (該監聽器能實現 加載圖片取消時,失敗時的方法)
ImageLoadingProgressListener
最後在監聽器的onLoadingComplete方法裏,設置圖片顯示即可
進階
listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
第一個參數是imageLoader,第二個參數是滑動時是否加載圖片,第三個參數是猛的滑動時是否加載圖片
OOM問題
如果使用了ImageLoader出現OutOfMemoryError的話,那麽可以通過下列方法解決
關閉memory的cache。
減少線程池的大小
用Bitmap.Config.RGB565代替ARGB8888
使用.imageScaleType(ImageScaleType.EXACTLY 或ImageScaleType.IN_SAMPLE_INT)
使用.diskCacheExtraOption(480, 320, null)獲取緩存文件
1
2DiskCache diskCache = ImageLoader.getInstance().getDiskCache();
File cacheFile = DiskCacheUtils.findInCache(imgpath, diskCache);
ImageView ScaleType
ImageLoader的使用及封裝
使用
1.全局初始化ImageLoader,配置ImageLoader的參數(ImageLoaderConfiguration)
2.配置圖片加載的參數(DisplayImageOptions)
3.創建ImageLoader的對象,調用displayImage方法
封裝
1.利用單例模式創建ImageLoaderTool的對象
1
|
public class ImageLoaderTool{
|
2.初始化ImageLoaderConfiguration
1
|
|
3.封裝DisplayImage方法
1
|
/**
|
示例 - 封裝ImageLoader
1
|
/**
|
使用ImageLoader時加載相同URL的問題
//使用displayImage,而不使用loadImage,因為loadImage會導致當同時加載同一個url的時候出現task被取消的情況
//詳情見https://github.com/nostra13/Android-Universal-Image-Loader/issues/804
1
|
// 解決方法
|
解決listvie寬度設置wwrap_content無效的方法
stackoverflow大佬
現象
listview寬度設置wrap_content無效
解決辦法,添加FrameLayout跟LinearLayout設置weight
修改前代碼
1
|
<ListView
|
修改後代碼
1
|
<LinearLayout
|
本文標題:ImageLoader的理解
文章作者:TanJunDang
發布時間:2015年09月17日 - 00時00分
最後更新:2018年05月14日 - 15時44分
原始鏈接:http://TanJunDang.github.io/2015/09/17/ImageLoaderTool/
https://tanjundang.github.io/2015/09/17/ImageLoaderTool/
ImageLoader作用 AAAA