开源框架利与弊
开源框架给开发者提供了便利,避免了重复造轮子,但是却隐藏了一些开发上的细节,如果不关注其内部实现,那么将不利于开发人员掌握核心技术,当然也谈不上更好的使用它,计划分析项目的集成使用和低层实现。
基本四部曲
- imageLoaderConfiguration
- ->imageLoader.init(imageLoaderConfiguration)
- displayImageOptions
- ->imageLoader.displayImage(...,displayImageOptions,...)
1. ImageLoaderConfiguration(有默认)
通过ImageLoaderConfiguration进行配置
两种方式:
- 默认
ImageLoaderConfiguration configuration = ImageLoaderConfiguration
.createDefault(this);
- 详细的
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/AAAAAAA