ImageLoader 详解


应用情景

  • 网络加载大量图片

注意事项

  • 1.自定义 application 必须在清单文件只能配置 Application android:name 属性
    1. 因图片等资源在网络中加载,需配置 internet 权限
    1. 因设置本地缓存,会在本地写入内容,需要配置 写SD卡的权限
  • 4.一般我们在使用ImageLoader的时候,需要在应用程序的入口进行它的一个配置,这个配置一般写到Application里边
    1. 在Application 和 Activity 中都用了 ImageLoader,在Application中通过 ImageLoader.getInstance().init(ImageLoaderConfiguration config),来初始化 ImageLoader 操作;而在Activity 中通过 ImageLoader.getInstance().displayImage(String url, ImageView iv,DisplayImageOptions options)来设置图片。可以看到共同点:ImageLoader.getInstance(),其后为init()和display()分别执行初始化和展示操作

注意方法

  • getInstance()

    得到ImageLoader的单例。通过双层是否为 null 判断提高性能

  • init(ImageLoaderConfiguration configuration)

    初始化配置参数,参数configuration为ImageLoader的配置信息,包括图片最大尺寸、任务线程池、磁盘缓存、下载器、解码器等等。
    实现中会初始化ImageLoaderEngine engine属性,该属性为任务分发器。

  • displayImage(String uri, ImageAware imageAware, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener)
    加载并显示图片或加载并执行回调接口

使用详解__步骤

    1. 导入安装包 universal-image-loader-1.9.3.jar
  • 2.在app 初始化的时候设置 ImageLoaderConfiguration,并初始化 ImageLoader(采用自定义的 Application ,在其里面的 onCreate()方法里面进行处理)

    • 2.1 获取 ImageLoaderConfiguration 对象的内部类 Builder
      ImageLoaderConfiguration.Builder builder = new Builder(
      getApplicationContext());
    • 2.2 设置相关参数

      • builder.threadPoolSize(3);// 设置线程数量
      • builder.threadPriority(Thread.NORM_PRIORITY - 2);// 设置线程优先级
      • builder.memoryCacheSize(2 * 1024 * 1024);// 设置缓存空间
      • builder.memoryCacheExtraOptions(480, 800);// 设置缓存中的图片宽高
      • builder.diskCacheSize(50 * 1024 * 1024); // 设置缓存空间 50M (磁盘中)
      • builder.diskCache(new UnlimitedDiscCache(new File(path)));// 自定义文件的缓存路径
        • String path=Environment.getExternalStorageDirectory().getAbsolutePath()+”…”;
      • builder.diskCacheFileNameGenerator(new Md5FileNameGenerator());//磁盘缓存的文件名的命名方式

      • 一般使用默认值 (获取文件名称的hashcode然后转换成字符串)
        或MD5 new Md5FileNameGenerator()源文件的名称同过md5加密后保存
    • builder.denyCacheImageMultipleSizesInMemory();

    • builder.tasksProcessingOrder(QueueProcessingType.LIFO);

      加载同一URL图片是,imageView 从小变大 ,从内存缓存中加载

  • 2.3 获取ImageLoaderConfiguration 对象
    • ImageLoaderConfiguration config = builder.build();
  • 2.4 初始化 ImageLoader
    • ImageLoader.getInstance().init(config);
  • 3.在活动中设置 DisplayImageOptions

    • 3.1获取 DisplayImageOptions 对象的内部类 Builder

      • DisplayImageOptions.Builder builder=new Builder();
    • 3.2 设置使用缓存 在内存中
      • builder.cacheInMemory(true);
    • 3.3 设置使用缓存在 SD 卡中
      • builder.cacheOnDisc(true);
    • 3.4 其他设置 非必须 默认图片 在 drawable中进行加载
      • 设置图片为空时,默认图片

        • builder.showImageForEmptyUri(R.drawable.ic_empty);
      • 设置加载失败的默认图片
        • builder.showImageOnFail(R.drawable.ic_error);
      • 设置正在加载的默认图片
        • builder.showImageOnLoading(R.drawable.ic_stub);
      • builder设置圆角
        • builder.displayer(new RoundedBitmapDisplayer(20));
    • 3.5 获取 DisplayImageOptions 对象
      • DisplayImageOptions options = builder.build();
  • 4.展示图片
    • 可设置样式 ImageLoader.getInstance().displayImage(String url, ImageView iv,DisplayImageOptions options);
    • 默认样式 ImageLoader.getInstance().displayImage(String url, ImageView iv)
  • 5.相关配置
    • 权限配置

      • 访问Intent _
      • 写内存卡 __
    • application 配置
      • 使用自定义的 Application,将默认application设置改为自定义的application,重新配置name,android:name=”com.example.and4_imageloader_demo1.MyApplication”
  • ImageLoader 配置介绍

      1. ImageLoaderConfiguration:

      是针对图片缓存的全局配置,主要有线程类、缓存大小、磁盘大小、图片下载与解析、日志方面的配置

      1. DisplayImageOptions:

      用于指导每一个Imageloader根据网络图片的状态(空白、下载错误、正在下载)显示对应的图片,是否将缓存加载到磁盘上,下载完后对图片进行怎么样的处理

      1. ImageLoader:

      是具体下载图片,缓存图片,显示图片的具体执行类,它有两个具体的方法displayImage(…)、loadImage(…),但是其实最终他们的实现都是displayImage(…)

    从三者的协作关系上看,他们有点像厨房规定、厨师、客户个人口味之间的关系ImageLoaderConfiguration就像是厨房里面的规定,每一个厨师要怎么着装,要怎么保持厨房的干净,这是针对每一个厨师都适用的规定,而且不允许个性化改变。ImageLoader就像是具体做菜的厨师,负责具体菜谱的制作。DisplayImageOptions就像每个客户的偏好,根据客户是重口味还是清淡,每一个imageLoader根据DisplayImageOptions的要求具体执行

    相关概念

    • 为Google 为 Android 所做的开源框架
    • 下载地址 https://github.com/nostra13/Android-Universal-Image-Loader
    • 安装包 universal-image-loader-1.9.3.jar

      Android Universal Image Loader 是一个强大的、可高度定制的图片缓存

    多线程异步加载和显示图片(图片来源于网络、sd卡、assets文件夹,drawable文件夹(不能加载9patch),新增加载视频缩略图)

    特点

    • 可配置高。支持任务线程池、下载器、解码器、内存和磁盘的缓冲、显示选项等等的配置。 (ImageLoaderConfiguration 里面配置)
    • 包含内存缓冲和磁盘缓冲两级缓冲。(ImageLoaderConfiguration、DisplayImageOptions 里面配置)
    • 支持多线程,支持异步和同步加载(ImageLoaderConfiguration 里面配置)
    • 支持多种缓冲算法、下载进度监听、ListView图片错乱解决等。???

    出现背景

    面临问题
    • 我们经常会从网络(SD卡)中加载大量的图片,如果处理不好,经常会出现内存溢出(OutOfMemoryError),导致app崩溃,还有下载速度慢等问题…

    常用解决方式:

      1. 对图片进行压缩(BitmapFactory.Options)
      1. 及时回收 Bitmap 的内存(Bitmap.recyle());
      1. 缓存图片(缓存到内存、SD卡等[Map

    更好的解决方案

    • ImageLoader 基本处理了该类问题,避免了这些问题,下载速度快,还有很好的缓冲管理机制


    示例代码

    布局文件

    activity_main.xml
    `<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListViewandroid:id="@+id/listView"android:layout_width="match_parent"android:layout_height="match_parent"/>
    </RelativeLayout>`
    
    list_item.xml
    `<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <ImageView android:id="@+id/iv"android:layout_width="72dp"android:layout_height="72dp"android:src="@drawable/ic_launcher"/>
    <TextView android:id="@+id/tv"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="测试"android:layout_gravity="center"/>
    </LinearLayout>`
    

    自定义 Application

    `import java.io.File;
    import android.app.Application;
    import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
    import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
    import com.nostra13.universalimageloader.core.ImageLoader;
    import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
    import com.nostra13.universalimageloader.core.ImageLoaderConfiguration.Builder;
    import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
    public class MyApplication extends Application {
    @Override
    public void onCreate() {super.onCreate();ImageLoaderConfiguration.Builder builder = new Builder(getApplicationContext());builder.threadPoolSize(3);// 设置线程数量builder.threadPriority(Thread.NORM_PRIORITY - 2);// 设置线程优先级builder.memoryCacheSize(2 * 1024 * 1024);// 设置缓存空间// 设置缓存中的图片宽高builder.memoryCacheExtraOptions(480, 800);// 设置缓存空间 50M (磁盘中)builder.diskCacheSize(50 * 1024 * 1024);// 自定义文件的缓存路径builder.diskCache(new UnlimitedDiscCache(new File("")));//磁盘缓存的文件名的命名方式//一般使用默认值 (获取文件名称的hashcode然后转换成字符串)//或MD5 new Md5FileNameGenerator()源文件的名称同过md5加密后保存builder.diskCacheFileNameGenerator(new Md5FileNameGenerator());//加载同一URL图片是,imageView 从小变大 ,从内存缓存中加载builder.denyCacheImageMultipleSizesInMemory();builder.tasksProcessingOrder(QueueProcessingType.LIFO);ImageLoaderConfiguration config = builder.build();//2. 初始化 ImageLoaderImageLoader.getInstance().init(config);
    }}`
    

    图片网络地址

    `package com.example.and4_imageloader_demo1;public class Constants {public static final String IMAGES = "CONSTANTS.IMAGES";
    public static final String IMAGE_POSITION = "CONSTANTS.IMAGE_POSITION";public static final String[] images = new String[] {"http://cdn.duitang.com/uploads/blog/201308/18/20130818150526_Ru2Bk.thumb.600_0.png","http://www.bkill.com/u/info_img/2012-09/02/2012083116140522302.jpg","http://www.it165.net/uploadfile/2011/1218/20111218070928328.jpg","http://www.3761.com/uploads/pic/42861391998071.jpg","http://www.jhq8.cn/qqtouxiang/UploadPic/2012-9/201291016107737.jpg","http://www.3761.com/uploads/pic/71781391998073.jpg","http://www.qqcan.com/uploads/allimg/c120822/13455c923250-91E45.jpg","http://p1.qq181.com/cms/120503/2012050320291269450.jpg","http://www.qqbody.com/uploads/allimg/201401/20-094917_95.jpg","http://www.qqbody.com/uploads/allimg/201301/15-200525_65.jpg","http://www.qqgqtx.com/uploads/allimg/130117/1-13011F61119.jpg","http://www.xk77.com/uploads/allimg/111115/1_111115124125_6.jpg","http://www.3761.com/uploads/pic/7761379903040.jpg","http://www.qqai.net/fa/UploadPic/2012-8/2012829161638939.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqya.com/userimg/2455/110514234111.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://img1.touxiang.cn/uploads/20121125/25-032807_173.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://img1.touxiang.cn/uploads/20121204/04-013352_840.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://img1.touxiang.cn/uploads/20121204/04-013953_608.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqphotos.com/uploads/allimg/1401/2-140112112226.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-130326094313-52.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.3761.com/uploads/pic/32201377656359.png","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qjis.com/uploads/allimg/121027/130Z635X-3.png","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130928110958_zmzdn.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.jhq8.cn/qqtouxiang/UploadPic/2012-11/2012119154647323.jpg","http://www.qqbody.com/uploads/allimg/201301/12-202555_717.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg","http://www.qqw21.com/article/UploadPic/2012-8/20128923812441.jpg","http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130111/1-130111210R4.png","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.bkill.com/u/info_img/2012-09/02/2012083116140516694.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.qqgqtx.com/uploads/allimg/130320/1-1303200ZZ0.jpg","http://www.qqbody.com/uploads/allimg/201302/20-143539_945.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210553_4kr7a.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20130521210555_iexeg.jpg","http://www.qqgqtx.com/uploads/allimg/130326/1-1303261A329-51.jpg","http://www.meilinvsheng.com/upload_files/article/135/1_20121202211215_fnoal.jpg"
    };
    }
    `
    

    活动代码

    `import java.util.ArrayList;
    import java.util.List;
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    import com.nostra13.universalimageloader.core.DisplayImageOptions;
    import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder;
    import com.nostra13.universalimageloader.core.ImageLoader;
    import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
    public class MainActivity extends Activity {private ListView listView;
    private List<String> list;@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);list = new ArrayList<String>();String[] str = Constants.images;for (String string : str) {list.add(string);}//设置    DisplayImageOptionsDisplayImageOptions.Builder builder=new Builder();//设置图片为空时,默认图片builder.showImageForEmptyUri(R.drawable.ic_empty);//设置加载失败的默认图片builder.showImageOnFail(R.drawable.ic_error);//设置正在加载的默认图片builder.showImageOnLoading(R.drawable.ic_stub);//设置使用缓存 在内存中builder.cacheInMemory(true);//设置使用缓存在 SD 卡中builder.cacheOnDisc(true);//builder设置圆角builder.displayer(new RoundedBitmapDisplayer(20));DisplayImageOptions options = builder.build();listView = (ListView) findViewById(R.id.listView);listView.setAdapter(new MyAdapter(MainActivity.this, list,options));
    }class MyAdapter extends BaseAdapter {private Context context;private List<String> list;private DisplayImageOptions options;public MyAdapter(Context context, List<String> list, DisplayImageOptions options) {this.context = context;this.list = list;this.options=options;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn list.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;if (convertView == null) {holder = new ViewHolder();convertView = View.inflate(context, R.layout.list_item, null);holder.iv = (ImageView) convertView.findViewById(R.id.iv);holder.tv = (TextView) convertView.findViewById(R.id.tv);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}ImageLoader.getInstance().displayImage(list.get(position), holder.iv,options);holder.tv.setText("item" + position);return convertView;}class ViewHolder {private ImageView iv;private TextView tv;}}}`
    

    配置清单文件

    `<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.and4_imageloader_demo1"
    android:versionCode="1"
    android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/><application  android:name="com.example.and4_imageloader_demo1.MyApplication"android:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activity            android:name="com.example.and4_imageloader_demo1.MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity>
    </application>
    </manifest>`
    

    关联源码 参考v4 包中 ViewPager ,相同的方式

    可成功:5.1.删除 libs-->android-support-v4.jar ,
    进入 JavaBuildPath -->Libraries --->选择 Android Dependencies -->Remove
    5.2.右击项目 ---> Properties    进入 JavaBuildPath -->Libraries-->AddExternal JARs..
    选择 sdk\extras\android\support\v4  android-support-v4.jar,点击OK
    5.3进入 JavaBuildPath -->Order and Export --->勾选 Android 6.0 ,android-support-v4.jar
    点击AttachSource --->External Folder-->选择sdk\extras\android\support\v4路径下 android-support-v4.jar,点击OK.可成功:
    5.1 右击项目 ---> Properties    进入 JavaBuildPath -->Libraries-->选择 Android Dependencies -->Remove
    5.2 右击 libs-->android-support-v4.jar -----》BuildPath
    5.3  进入 JavaBuildPath -->Order and Export --->勾选 Android 6.0 ,android-support-v4.jar
    

    提升 源码

    init方法

    `public synchronized void init(ImageLoaderConfiguration configuration) {if (configuration == null) {throw new IllegalArgumentException(ERROR_INIT_CONFIG_WITH_NULL);}if (this.configuration == null) {L.d(LOG_INIT_CONFIG);engine = new ImageLoaderEngine(configuration);this.configuration = configuration;} else {L.w(WARNING_RE_INIT_CONFIG);}
    }
    `
    

    getInstance(); 如何保证为单例模式???

    `   public static ImageLoader getInstance() {if (instance == null) {synchronized (ImageLoader.class) {if (instance == null) {instance = new ImageLoader();}}}return instance;
    }`
    

    ImageLoaderEngine()方法

    `   ImageLoaderEngine(ImageLoaderConfiguration configuration) {this.configuration = configuration;taskExecutor = configuration.taskExecutor;taskExecutorForCachedImages = configuration.taskExecutorForCachedImages;taskDistributor = DefaultConfigurationFactory.createTaskDistributor();
    }`
    

    displayImage()方法

    `public void displayImage(String uri, ImageView imageView, DisplayImageOptions options) {displayImage(uri, new ImageViewAware(imageView), options, null, null);
    }`
    

    displayImage 深入源码

    `public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options,ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {checkConfiguration();if (imageAware == null) {throw new IllegalArgumentException(ERROR_WRONG_ARGUMENTS);}if (listener == null) {listener = emptyListener;}if (options == null) {options = configuration.defaultDisplayImageOptions;}if (TextUtils.isEmpty(uri)) {engine.cancelDisplayTaskFor(imageAware);listener.onLoadingStarted(uri, imageAware.getWrappedView());if (options.shouldShowImageForEmptyUri()) {imageAware.setImageDrawable(options.getImageForEmptyUri(configuration.resources));} else {imageAware.setImageDrawable(null);}listener.onLoadingComplete(uri, imageAware.getWrappedView(), null);return;}ImageSize targetSize = ImageSizeUtils.defineTargetSizeForView(imageAware, configuration.getMaxImageSize());String memoryCacheKey = MemoryCacheUtils.generateKey(uri, targetSize);engine.prepareDisplayTaskFor(imageAware, memoryCacheKey);listener.onLoadingStarted(uri, imageAware.getWrappedView());Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);if (bmp != null && !bmp.isRecycled()) {L.d(LOG_LOAD_IMAGE_FROM_MEMORY_CACHE, memoryCacheKey);if (options.shouldPostProcess()) {ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey,options, listener, progressListener, engine.getLockForUri(uri));ProcessAndDisplayImageTask displayTask = new ProcessAndDisplayImageTask(engine, bmp, imageLoadingInfo,defineHandler(options));if (options.isSyncLoading()) {displayTask.run();} else {engine.submit(displayTask);}} else {options.getDisplayer().display(bmp, imageAware, LoadedFrom.MEMORY_CACHE);listener.onLoadingComplete(uri, imageAware.getWrappedView(), bmp);}} else {if (options.shouldShowImageOnLoading()) {imageAware.setImageDrawable(options.getImageOnLoading(configuration.resources));} else if (options.isResetViewBeforeLoading()) {imageAware.setImageDrawable(null);}ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey,options, listener, progressListener, engine.getLockForUri(uri));LoadAndDisplayImageTask displayTask = new LoadAndDisplayImageTask(engine, imageLoadingInfo,defineHandler(options));if (options.isSyncLoading()) {displayTask.run();} else {engine.submit(displayTask);}}
    }
    

    `

ImageLoader 详解相关推荐

  1. Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解

    原文地址为: Android-Universal-Image-Loader三大组件DisplayImageOptions.ImageLoader.ImageLoaderConfiguration详解 ...

  2. android组件用法说明,Android第三方控件PhotoView使用方法详解

    Android第三方控件PhotoView使用方法详解 发布时间:2020-10-21 15:06:09 来源:脚本之家 阅读:74 作者:zhaihaohao1 PhotoView的简介: 这是一个 ...

  3. python数据处理常用函数_pytorch中的自定义数据处理详解

    pytorch在数据中采用Dataset的数据保存方式,需要继承data.Dataset类,如果需要自己处理数据的话,需要实现两个基本方法. :.getitem:返回一条数据或者一个样本,obj[in ...

  4. python构造自定义数据包_pytorch中的自定义数据处理详解

    pytorch在数据中采用Dataset的数据保存方式,需要继承data.Dataset类,如果需要自己处理数据的话,需要实现两个基本方法. :.getitem:返回一条数据或者一个样本,obj[in ...

  5. Android第三方登录详解2

    接着Android第三方登录详解1讲 1.找到友盟  文档中心 2.找到 3.将 UMSocialService mController = UMServiceFactory.getUMSocialS ...

  6. memoryCache和diskCache流程详解

    Webkit的MemoryCache和DiskCache流程详解 MemoryCache简介: MemoryCache顾名思义,就是将资源缓存到内存中,等待下次访问时不需要重新下载资源,而直接从内存中 ...

  7. Android客户端实现注册/登录详解(一)

    前言 我们在开发安卓App时难免要与服务器打交道,尤其是对于用户账号信息的注册与登录更是每个android开发人员必须掌握的技能,本文将对客户端的注册/登录功能的实现进行分析,不到之处还请指出. 在这 ...

  8. Java多线程进阶详解

    文章目录 1.卖票案例引入数据不安全问题 2.同步代码块 深入理解synchronized关键字 3.同步方法与静态同步方法 同步方法 静态同步方法 内置锁 静态同步方法与同步代码块共同使用 为什么要 ...

  9. Google官方 详解 Android 性能优化【史诗巨著之内存篇】

    尊重博主原创,如需转载,请附上本文链接http://blog.csdn.net/chivalrousman/article/details/51553114#t16 为什么关注性能 对于一款APP,用 ...

最新文章

  1. Objective-C市场占有率排名升至第4位
  2. 妨碍你成为CCIE的10个不良习惯
  3. aarch64的TCR寄存器介绍
  4. Vue本地执行build之后打开dist目录下index.html正常访问
  5. Permission denied (publickey). fatal: Could not read from remote repository.
  6. python邮件发送csv附件_Python2.7 smtplib发送带附件邮件报错STARTTLS解决方法
  7. JavaScript文档对象模型document对象查找Html元素(2)
  8. 【STM32Cube笔记】4-STM32Cube配置时钟设置
  9. 壮观性能服务器图片介绍,配至强7500 图解惠普ProLiant DL980 G7
  10. 【Tools系列】OneNote 2016 中同步笔记时出现0xE4010640错误
  11. java实现文件的加密,Java实现文件的加密解密功能示例
  12. 防止恶意调用API接口
  13. jquery的设置多个 CSS 属性
  14. Siege 压力测试
  15. 启动计算机加载状态监控器,状态监控器显示脱机。
  16. 软件工程与计算II-8-软件设计基础
  17. 最常用计算机机箱,好看又实用 给你的电脑选一个好机箱
  18. 【python】又拍云采集工具助手exe带python图片采集源码
  19. 美国在线教育进入细分时代:VR课堂、AI聊天机器人、在线高中出现
  20. 如何解决升级macOS Big Sur后HandOff 接力功能失效?

热门文章

  1. 两个函数相加、相减、相乘等之后的单调性
  2. 基于Mozilla平台的扩展开发(续)----XPCOM组件篇
  3. 函数打桩原理_难重现问题定位“函数打桩”
  4. 软件工程----第一遍机房文档之串思路
  5. mp3转wma格式转换器 mp3音频怎么转换wma格式
  6. 基于OP放大器的有源模拟滤波器设计--基础知识
  7. TP-link WR703N v1.17固件不拆机绕过RSA验证强刷openwrt
  8. 批处教程 for /f 中的Delims和Tokens总结
  9. 【找工作】三大运营商、航十
  10. 短视频文案怎么写?优质短视频文案写作技巧