1. 程式人生 > >ImageLoader作用 AAAA

ImageLoader作用 AAAA

tbb u2l xtra delay 實現 監聽 con aop 進度

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
2
<uses-permission android:name="android.permission.INTERNET" />  
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

ImageLoader加載的類型

1
2
3
4
5
6
7
"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 [用imageView.setImageResource(drawable)來代替]

ImageLoader的加載、顯示配置:

加載Image時的配置(ImageLoaderConfiguration)

自定義配置 - 通過new ImageLoaderConfiguration.Builder().builder()方法進行實例化。
默認配置 - 通過ImageLoaderConfiguration 的createDefault進行實例化。

  • 默認配置
1
2
3
4
5
6
7
8
9
File diskCache = StorageUtils.getOwnCacheDirectory(context, "BNJ_IMAGE_CACHE/");  
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.diskCache(new UnlimitedDiscCache(diskCache)) // 設置緩存路徑
.memoryCacheSize(2 * 1024) // 設置緩存的最大字節
.diskCacheFileNameGenerator(new Md5FileNameGenerator()) // 加密
.denyCacheImageMultipleSizesInMemory() // 禁止緩存顯示不同大小的同一張圖片
.memoryCacheExtraOptions(800, 760) // 保存每個緩存圖片的最大長和寬
.diskCacheFileCount(100) //緩存的File數量
.build();

使用
通過ImageLoader的getInstance().init()方法傳入上述options對象.

顯示Image時的配置(DisplayImageOptions)

自定義配置 - 通過new DisplayImageOptions.Builder().builder()方法實例化對象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
DisplayImageOptions options = new DisplayImageOptions.Builder()  
.showImageOnLoading(R.drawable.ic_stub) // 設置加載時的圖片 (使用loadImage方法加載時無效)
.showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable
.showImageOnFail(R.drawable.ic_error) // 設置加載失敗的圖片
.cacheInMemory(true) //設置使用內存緩存
.cacheOnDisk(true) //使用文件緩存
.displayer(new RoundedBitmapDisplayer(20)) // 設置成圓角圖片
.bitmapConfig(Bitmap.Config.RGB_565) //減少內存消耗
.delayBeforeLoading(100) //設置下載圖片前的延時
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
.considerExifParams(false) //是否考慮JPEG圖像EXIF參數(旋轉,翻轉)
.build();

ImageSize size= new ImageSize(300,300);(大小可以通過創建ImageSize對象設置)

上述圖片顯示displayer
//顯示圓角 RoundedBitmapDisplayer
//淡入顯示 FadeInBitmapDisplayer

加載圖片的方法

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
2
DiskCache 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class ImageLoaderTool{

private static ImageLoader imageLoader;//用於全局初始化
private static ImageLoaderConfiguration configuration;//配置ImageLoader的參數

private static class Holder{ //單例模式
private static final ImageLoaderTool imageLoaderTool = new ImageLoaderTool();
}

public static ImageLoaderTool getInstance(){
if(configuration==null || imageLoader==null){
throw new RunTimeException ("Must be call the method InitImageLoader(Context context) in Application");
}
return Holder.imageLoaderTool;
}
}

2.初始化ImageLoaderConfiguration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

public static void initImageLoader(Context context) {
File diskCache = StorageUtils.getOwnCacheDirectory(context, "TJD_NBA/");
if (configuration == null) {
configuration = new ImageLoaderConfiguration.Builder(context)
.diskCache(new UnlimitedDiskCache(diskCache))
.diskCacheFileNameGenerator(new Md5FileNameGenerator()) //md5加密
.memoryCacheSize(1024 * 2)//緩存最大字節
.memoryCache(new LruMemoryCache(5 * 1024 * 1024))//解決緩存listview滑動後顯示默認圖
.memoryCacheExtraOptions(800, 760)// 保存每個緩存圖片的最大長和寬
.diskCacheFileCount(100) //緩存的File數量
.build();
}
if (imageLoader == null) {
imageLoader = ImageLoader.getInstance();
imageLoader.init(configuration);
}
}

3.封裝DisplayImage方法

1
2
3
4
5
6
7
8
9
/**
* 加載drawable圖片,不能加載系統的drawable 即android:drawable
*
* @param resid
* @param imageView
*/
public void loadImageFromDrawable(int resid, ImageView imageView) {
imageLoader.displayImage(DRAWABLE_PREFIX + resid, imageView);
}

示例 - 封裝ImageLoader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* ImageLoader的封裝。單例模式
* 1. "http://site.com/image.png" // from Web
* 2. "file:///mnt/sdcard/image.png" // from SD card
* 3. "file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail)
* 4. "content://media/external/images/media/13" // from content provider
* 5. "content://media/external/video/media/13" // from content provider (video thumbnail)
* 6. "assets://image.png" // from assets
* 7. "drawable://" + R.drawable.img // from drawables (non-9patch images)[用imageView.setImageResource(drawable)來代替]
*/
public class ImageLoaderTool {

private static ImageLoader imageLoader; //用於全局初始化
private static ImageLoaderConfiguration configuration;//默認的ImageLoader配置
private final String DRAWABLE_PREFIX = "drawable://";//用於加載drawable圖像

private static class Holder {
private static final ImageLoaderTool loaderTool = new ImageLoaderTool();
}

public static ImageLoaderTool getInstance() {
if (imageLoader == null || configuration == null) {
throw new RuntimeException("Must be call the method InitImageLoader(Context context) in Application");
}
return Holder.loaderTool;
}

//
// 初始化ImageLoader
//
// @param context 在APP中
//
public static void initImageLoader(Context context) {
File diskCache = StorageUtils.getOwnCacheDirectory(context, "TJD_NBA/");
if (configuration == null) {
configuration = new ImageLoaderConfiguration.Builder(context)
.diskCache(new UnlimitedDiskCache(diskCache))
.diskCacheFileNameGenerator(new Md5FileNameGenerator()) //md5加密
.memoryCacheSize(1024 * 2)//緩存最大字節
.memoryCache(new LruMemoryCache(5 * 1024 * 1024))//解決緩存listview滑動後顯示默認圖
.memoryCacheExtraOptions(800, 760)// 保存每個緩存圖片的最大長和寬
.diskCacheFileCount(100) //緩存的File數量
.build();
}
if (imageLoader == null) {
imageLoader = ImageLoader.getInstance();
imageLoader.init(configuration);
}
}

//
// 默認圖片加載參數
//
private static DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.EXACTLY)
.showImageOnFail(R.drawable.side_nav_bar)
.showImageForEmptyUri(R.drawable.side_nav_bar)
.showImageOnLoading(R.drawable.side_nav_bar)
.delayBeforeLoading(1000)
.considerExifParams(true) //是否考慮JPEG圖像EXIF參數(旋轉,翻轉)
.build();

//
// 加載普通圖片
//
// @param url
// @param imageView
//
public void loadImage(String url, ImageView imageView) {
imageLoader.displayImage(url, imageView, defaultOptions);
}

//
// 加載drawable圖片,不能加載系統的drawable 即android:drawable
//
// @param resid
// @param imageView
//
public void loadImageFromDrawable(int resid, ImageView imageView) {
imageLoader.displayImage(DRAWABLE_PREFIX + resid, imageView);
}

}

使用ImageLoader時加載相同URL的問題

//使用displayImage,而不使用loadImage,因為loadImage會導致當同時加載同一個url的時候出現task被取消的情況
//詳情見https://github.com/nostra13/Android-Universal-Image-Loader/issues/804

1
2
3
// 解決方法
ImageSize size = new ImageSize(Functions.getScreenWidthPix(getActivity()), Functions.getScreenHeightPix(getActivity()));
NonViewAware imageAware = new NonViewAware(size, ViewScaleType.CROP);

解決listvie寬度設置wwrap_content無效的方法

stackoverflow大佬

現象
listview寬度設置wrap_content無效
解決辦法,添加FrameLayout跟LinearLayout設置weight

修改前代碼

技術分享圖片

1
2
3
4
5
6
7
8
9
10
11
12
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:background="黑色"
android:divider="@null"
android:gravity="center"
android:horizontalSpacing="7dp"
android:numColumns="4"
android:paddingLeft="@dimen/common_layout_margin_15dp"
android:paddingRight="@dimen/common_layout_margin_15dp"></ListView>

修改後代碼

技術分享圖片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:background="黑色"
android:divider="@null"
android:gravity="center"
android:horizontalSpacing="7dp"
android:numColumns="4"
android:paddingLeft="@dimen/common_layout_margin_15dp"
android:paddingRight="@dimen/common_layout_margin_15dp"></ListView>

<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:background="藍色" />

</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