Universal-Image-Loader 使用步骤

开源框架利与弊

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

基本四部曲

  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
原文地址:https://www.cnblogs.com/Mihai/p/5775946.html