设为首页 加入收藏

TOP

Universal-Image-Loader 使用步骤(一)
2017-10-13 10:35:56 】 浏览:5812
Tags: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/AAAAAAA
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android 绘制view的小知识点 下一篇listview中button抢占焦点问题

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目