系类文章:(universal Image Loader)UIL使用(2),UIL使用3
universal image loader 的功能就是加载图片
在as 中 app 模块下的 build.gradle 文件中加上 依赖 (不了解Gradle的可以看看这个基本的Gradle介绍) Android Studio 之Gradle 学习
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
当然也可以下载jar 包 放在libs 目录下
然后再android manifest 文件下加入下面的权限
<manifest> <!-- Include following permission if you load images from Internet 联网权限--> <uses-permission android:name="android.permission.INTERNET" /> <!-- Include following permission if you want to cache images on SD card 写sd 权限--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ... </manifest>在第一次使用UIL加载图片之前必须先在Application /Activity中创建一个全局的配置和用这个配置初始化ImageLoader
例如像下面这样
public class MyApplication extends Application { private static ImageLoader mImageLoader; @Override public void onCreate() { super.onCreate(); /** * 全局配置 ,这里简单的使用createDefault() 创建一个默认的imageLoaderConfiguration * 等同于 ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(this).build(); * */ ImageLoaderConfiguration imageLoaderConfiguration = ImageLoaderConfiguration.createDefault(this); //初始化ImageLoader ImageLoader.getInstance().init(imageLoaderConfiguration); mImageLoader = ImageLoader.getInstance(); } public static ImageLoader getImageLoader() { return mImageLoader; } }
MainActivity 中
public class MainActivity extends AppCompatActivity { private final String url = "http://tupian.enterdesk.net/2015/0815/20150815044936402.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = (ImageView) this.findViewById(R.id.imageView); //获得在Application中配置初始化的Imageloader ImageLoader imageLoader = MyApplication.getImageLoader(); //加载并显示图片在imageView中 imageLoader.displayImage(url,imageView); } }
运行效果
上面是最最简单的使用方法,easy
假如我在没有创建一个全局的配置和init ImageLoader 会有什么事情发生尼?
ImageLoaderConfiguration imageLoaderConfiguration = ImageLoaderConfiguration.createDefault(this); // // //初始化ImageLoader // ImageLoader.getInstance().init(imageLoaderConfiguration); mImageLoader = ImageLoader.getInstance();就是把上面的两行注释掉
追踪displayImage (ImageLoader.java)
会throws 一个非法状态的异常说在displayImage 方法条用之前这个init方法没有被调用
这个异常就是在checkConfiguration方法中throws 的
/** * Checks if ImageLoader's configuration was initialized * * @throws IllegalStateException if configuration wasn't initialized */ private void checkConfiguration() { if (configuration == null) { throw new IllegalStateException(ERROR_NOT_INIT); } }
这个configuration的赋值就是在 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); } }那么这个ImageLoaderConfiguration 类到底有什么作用对于加载图片的时候
看到这个class倒是不怎么大
里面有一个Builder 类
里面有一些变量
public static final int DEFAULT_THREAD_POOL_SIZE = 3;//线程池的大小
/** {@value} */
public static final int DEFAULT_THREAD_PRIORITY = Thread.NORM_PRIORITY - 2;线程优先级 = 正常优先级-2
/** {@value} */
public static final QueueProcessingType DEFAULT_TASK_PROCESSING_TYPE =
QueueProcessingType.FIFO; //线程的处理:先进先出
private Context context;
private int maxImageWidthForMemoryCache = 0;//在内存缓存中图片的最大宽度
private int maxImageHeightForMemoryCache = 0;//在内存缓存中图片的最大高度
private int maxImageWidthForDiskCache = 0;//在disk缓存中图片的最大宽度
private int maxImageHeightForDiskCache = 0;//在disk缓存中图片的最大高度度
private BitmapProcessor processorForDiskCache = null;//图片处理器,用于对从disk缓存中的取出来的图片进行处理
private Executor taskExecutor = null; //用于执行从源获取图片任务的 Executor
private Executor taskExecutorForCachedImages = null;//用于执行从cache获取图片任务的 Executor
private boolean customExecutor = false;//用户是否自定义了taskExecutor
private boolean customExecutorForCachedImages = false;//用户是否自定义了taskExecutorForCachedImages
private int threadPoolSize = DEFAULT_THREAD_POOL_SIZE;//线程池的大小
private int threadPriority = DEFAULT_THREAD_PRIORITY;//线程优先级
private boolean denyCacheImageMultipleSizesInMemory = false;//不允许缓存中有多张不同尺寸的图片
private QueueProcessingType tasksProcessingType = DEFAULT_TASK_PROCESSING_TYPE;//处理线程队列的方式FIFO ,LIFO
private int memoryCacheSize = 0; //内存缓存的大小
private long diskCacheSize = 0;//磁盘缓存的大小
private int diskCacheFileCount = 0;//磁盘缓存的文件数
private MemoryCache memoryCache = null;//内存缓存的对象
private DiskCache diskCache = null;//磁盘缓存 对象 ,一般sd卡
private FileNameGenerator diskCacheFileNameGenerator = null;//diskCache文件名生成的方法,如diskCacheFileNameGenerator 采用MD5加密
private ImageDownloader downloader = null;//图片下载器
private ImageDecoder decoder;//图片解释器
private DisplayImageOptions defaultDisplayImageOptions = null;//图片显示的相关配置,加载前,加载后,加载失败
private boolean writeLogs = false; //是否打开调试日志,记录UIL的详细Log
这个Builder 类里面的变量基本是包含了ImageLoaderConfiguration 类里面的变量
ImageLoaderConfiguration的构造方法
private ImageLoaderConfiguration(final Builder builder) {
resources = builder.context.getResources();
maxImageWidthForMemoryCache = builder.maxImageWidthForMemoryCache;
maxImageHeightForMemoryCache = builder.maxImageHeightForMemoryCache;
maxImageWidthForDiskCache = builder.maxImageWidthForDiskCache;
maxImageHeightForDiskCache = builder.maxImageHeightForDiskCache;
processorForDiskCache = builder.processorForDiskCache;
taskExecutor = builder.taskExecutor;
taskExecutorForCachedImages = builder.taskExecutorForCachedImages;
threadPoolSize = builder.threadPoolSize;
threadPriority = builder.threadPriority;
tasksProcessingType = builder.tasksProcessingType;
diskCache = builder.diskCache;
memoryCache = builder.memoryCache;
defaultDisplayImageOptions = builder.defaultDisplayImageOptions;
downloader = builder.downloader;
decoder = builder.decoder;
customExecutor = builder.customExecutor;
customExecutorForCachedImages = builder.customExecutorForCachedImages;
networkDeniedDownloader = new NetworkDeniedImageDownloader(downloader);
slowNetworkDownloader = new SlowNetworkImageDownloader(downloader);
L.writeDebugLogs(builder.writeLogs);
}
所以我们也可以自己配置全局配置
例如下面这样
File cacheDir = StorageUtils.getCacheDirectory(this); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this) .memoryCacheExtraOptions(480, 800) // default = device screen dimensions .diskCacheExtraOptions(480, 800, null) .taskExecutor(null) .taskExecutorForCachedImages(null) .threadPoolSize(3) // default .threadPriority(Thread.NORM_PRIORITY - 2) // default .tasksProcessingOrder(QueueProcessingType.FIFO) // default .denyCacheImageMultipleSizesInMemory() .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) .memoryCacheSize(2 * 1024 * 1024) .memoryCacheSizePercentage(13) // default .diskCache(new UnlimitedDiskCache(cacheDir)) // default .diskCacheSize(50 * 1024 * 1024) .diskCacheFileCount(100) .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default .imageDownloader(new BaseImageDownloader(this)) // default .imageDecoder(new BaseImageDecoder(true)) // default .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default .writeDebugLogs() .build();
那么我们使用的默认的configuration 的值是什么(createDefault方法)下面是ImageLoader.java里面的一段注释
Creates default configuration for {@link ImageLoader} <br />
<b>Default values:</b>
<ul>
<li>maxImageWidthForMemoryCache = device's screen width设备屏幕的宽度</li>
<li>maxImageHeightForMemoryCache = device's screen height设备屏幕的高度</li>
<li>maxImageWidthForDikcCache = unlimited没有限制</li>
<li>maxImageHeightForDiskCache = unlimited没有限制</li>
<li>threadPoolSize = public static final int DEFAULT_THREAD_POOL_SIZE = 3;</li>
<li>threadPriority = public static final int DEFAULT_THREAD_PRIORITY = Thread.NORM_PRIORITY - 2;</li>
<li>allow to cache different sizes of image in memory 允许同一张图片不同尺寸在内存的缓存中存在</li>
<li>memoryCache = 默认是LruMemoryCache</li>
<li>diskCache = 默认是UnlimitedDiskCache</li>
<li>imageDownloader = 默认BaseImageDownloader</li>
<li>imageDecoder =默认BaseImageDecoder(loggingEnabled);</li>
<li>diskCacheFileNameGenerator = 默认是HashCodeFileNameGenerator</li>
<li>defaultDisplayImageOptions = Simple options}</li>
<li>tasksProcessingOrder = 线程队列处理顺序 (还有LIFO)</li>
<li>detailed logging disabled 不打开调试日志</li>
</ul>
下面再看看getInstance()方法做了些什么
/** Returns singleton class instance
*得到ImageLoader的单例。通过双层是否为 null 判断提高性能。
*/
public static ImageLoader getInstance() {
if (instance == null) {
synchronized (ImageLoader.class) {
if (instance == null) {
instance = new ImageLoader();
}
}
}
return instance;
}
protected ImageLoader() {
}
未完代写.....