开源框架利与弊

开源框架给开发者提供了便利,避免了重复造轮子,但是却隐藏了一些开发上的细节,如果不关注其内部实现,那么将不利于开发人员掌握核心技术,当然也谈不上更好的使用它,计划分析项目的集成使用和低层实现。

基本四部曲

  1. imageLoaderConfiguration
  2. ->imageLoader.init(imageLoaderConfiguration)
  3. displayImageOptions
  4. ->imageLoader.displayImage(...,displayImageOptions,...)

1. ImageLoaderConfiguration(有默认)

通过ImageLoaderConfiguration进行配置
两种方式:

  1. 默认
ImageLoaderConfiguration configuration = ImageLoaderConfiguration
.createDefault(this);

  

  1. 详细的
File cacheDir = StorageUtils.getCacheDirectory(context);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.diskCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
.taskExecutor(...)
.taskExecutorForCachedImages(...)
.threadPoolSize(3) // default
.threadPriority(Thread.NORM_PRIORITY - 1) // default
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.memoryCacheSizePercentage(13) // default
.diskCache(new UnlimitedDiscCache(cacheDir)) // default
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileCount(100)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.imageDownloader(new BaseImageDownloader(context)) // default
.imageDecoder(new BaseImageDecoder()) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs()
.build();

  

2.给ImageLoader配置上一步信息

ImageLoader.getInstance().init(configuration);

ImageLoader使用单例模式:源码

public static  ImageLoader getInstance(){
/*为了线程安全加了锁
*一但ImageLoader对象有了,就直接跳过同步
*这是单例设计的思想(可以参考HeadFirst设计模式)
*/
if(instance == null){
synchorinized(ImageLoader.class){
if(instance == null){
instance = new ImageLoader();
}
}
}
return instance;
}

  

public synchronized void init(){
if(configuration == null){
throw new IlleagalArgumentException(ERROR_INIT_CONFIG_WITH_NULL);
if(this.configuration == null){
this.configuration = configuration;
emptyListener = new SimpleIageLoadingListener();
fakeBitmapDisplayer = new FakeBitmapDisplayer();
}
}
}

  

3.下载图片偏好

DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub) // resource or drawable
.showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable
.showImageOnFail(R.drawable.ic_error) // resource or drawable
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(1000)
.cacheInMemory(false) // default
.cacheOnDisk(false) // default
.preProcessor(...)
.postProcessor(...)
.extraForDownloader(...)
.considerExifParams(false) // default
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
.bitmapConfig(Bitmap.Config.ARGB_8888) // default
.decodingOptions(...)
.displayer(new SimpleBitmapDisplayer()) // default
.handler(new Handler()) // default
.build();

  

4.正式加载图片

两种方式:loadImage();displayImage()-一般用这个;

final ImageView mImageView = (ImageView) findViewById(R.id.image);
String imageUrl = "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg";
ImageSize mImageSize = new ImageSize(100, 100); //显示图片的配置
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build(); ImageLoader.getInstance().loadImage(imageUrl, mImageSize, options, new SimpleImageLoadingListener(){ @Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
mImageView.setImageBitmap(loadedImage);
} });

  

GirdView,ListView中使用

使用GridView,ListView来显示大量的图片,而当我们快速滑动GridView,ListView,我们希望能停止图片的加载,而在GridView,ListView停止滑动的时候加载当前界面的图片,这个框架当然也提供这个功能,使用起来也很简单,它提供了PauseOnScrollListener这个类来控制ListView,GridView滑动过程中停止去加载图片

listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
gridView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));

参考

  1. https://github.com/nostra13/Android-Universal-Image-Loader
  2. http://blog.csdn.net/xiaanming/article/details/26810303

最新文章

  1. 【踩坑速记】二次依赖?android studio编译运行各种踩坑解决方案,杜绝弯路,总有你想要的~
  2. APUE学习之多线程编程(三):线程属性、同步属性
  3. js中控制小数点的显示位数的技术整理
  4. iOS UITableView 分割线从零开始
  5. HDU 5883 The Best Path
  6. [Ext JS 4]性能优化
  7. bzoj1047/luogu2216 理想的正方形 (单调队列)
  8. 开源网站流量统计系统Piwik源码分析——后台处理(二)
  9. nginx for Windows Known issues:path
  10. CDH:cdh5环境搭建
  11. JS 引擎执行机制
  12. Atitit 常用sdk 模块 组织架构切分 规范与范例attilax总结
  13. ORA-30377 MV_CAPABILITIES_TABLE not found
  14. Paxos算法细节详解(一)
  15. 04python while循环语句
  16. 2018.10.22 cogs2471. [EZOI 2016]源氏的数学课(线段树)
  17. (并查集)Is It A Tree? --POJ--1308
  18. codeforces 17D Notepad
  19. 大型发布会现场的 Wi-Fi 应该如何搭建(密集人群部署wifi抗干扰)?
  20. 按照拼音排序的SQL语句条件

热门文章

  1. lsof 拥有更多的功能
  2. linux下解压缩jar包
  3. BZOJ2296: 【POJ Challenge】随机种子
  4. SIFT算法:确定特征点方向
  5. sql server 2005+ 翻页
  6. [转]CharacterController与Rigidbody
  7. 【转】Xcode7.1环境下上架iOS App到AppStore 流程 (Part 一)
  8. Ubuntu 使用apt-get时提示错误:无法获得锁 /var/lib/dpkg/lock
  9. [SDJX2015]面积
  10. Android学习笔记(八)深入分析Service启动、绑定过程