Android Imageloader的配置的实现代码

所属分类: 软件编程 / Android 阅读数: 41
收藏 0 赞 0 分享

Android Imageloader的配置的实现代码

  ImageLoader 优点

(1) 支持下载进度监听

(2) 可以在 View 滚动中暂停图片加载

通过 PauseOnScrollListener 接口可以在 View 滚动中暂停图片加载。

(3) 默认实现多种内存缓存算法 这几个图片缓存都可以配置缓存算法,不过 ImageLoader 默认实现了较多缓存算法,如 Size

最大先删除、使用最少先删除、最近最少使用、先进先删除、时间最长先删除等。

(4) 支持本地缓存文件名规则定义     

实现代码:


/** 
 * 初始化ImageLoader 
 */ 
public static void initImageLoader(Context context) { 
  File cacheDir = StorageUtils.getOwnCacheDirectory(context, 
      "bee_k77/Cache");// 获取到缓存的目录地址 
  Log.e("cacheDir", cacheDir.getPath()); 
  // 创建配置ImageLoader(所有的选项都是可选的,只使用那些你真的想定制),这个可以设定在APPLACATION里面,设置为全局的配置参数 
  ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( 
      context) 
      // max width, max height,即保存的每个缓存文件的最大长宽 
      .memoryCacheExtraOptions(480, 800) 
      // Can slow ImageLoader, use it carefully (Better don't use it)设置缓存的详细信息,最好不要设置这个 
/        .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)  
      // 线程池内加载的数量 
      .threadPoolSize(3) 
      // 线程优先级 
      .threadPriority(Thread.NORM_PRIORITY - 2) 
      /* 
       * When you display an image in a small ImageView 
       * and later you try to display this image (from identical URI) in a larger ImageView 
       * so decoded image of bigger size will be cached in memory as a previous decoded image of smaller size. 
       * So the default behavior is to allow to cache multiple sizes of one image in memory. 
       * You can deny it by calling this method: 
       * so when some image will be cached in memory then previous cached size of this image (if it exists) 
       *  will be removed from memory cache before. 
       */ 
/        .denyCacheImageMultipleSizesInMemory() 
       
      // You can pass your own memory cache implementation你可以通过自己的内存缓存实现 
      // .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))  
      // .memoryCacheSize(2 * 1024 * 1024) 
      //硬盘缓存50MB 
      .diskCacheSize(50 * 1024 * 1024) 
       //将保存的时候的URI名称用MD5 
      .diskCacheFileNameGenerator(new Md5FileNameGenerator()) 
      // 加密 
       .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())//将保存的时候的URI名称用HASHCODE加密 
      .tasksProcessingOrder(QueueProcessingType.LIFO) 
       .diskCacheFileCount(100) //缓存的File数量 
      .diskCache(new UnlimitedDiscCache(cacheDir))// 自定义缓存路径 
      // .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) 
      // .imageDownloader(new BaseImageDownloader(context, 5 * 1000, 
      // 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间 
      .writeDebugLogs() // Remove for release app 
      .build(); 
  // Initialize ImageLoader with configuration. 
  ImageLoader.getInstance().init(config);// 全局初始化此配置 
} 

Option类

package com.topnews.config; 
 
import android.graphics.Bitmap; 
 
import com.nostra13.universalimageloader.core.DisplayImageOptions; 
import com.nostra13.universalimageloader.core.assist.ImageScaleType; 
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; 
import com.topnews.R; 
 
public class Options { 
  /** 
   * 新闻列表中用到的图片加载配置 
   */ 
  public static DisplayImageOptions getListOptions() { 
    DisplayImageOptions options = new DisplayImageOptions.Builder() 
    // 设置图片在下载期间显示的图片 
        .showImageOnLoading(R.drawable.ic_stub) 
        // 设置图片Uri为空或是错误的时候显示的图片 
        .showImageForEmptyUri(R.drawable.ic_stub) 
        // 设置图片加载/解码过程中错误时候显示的图片 
        .showImageOnFail(R.drawable.ic_error) 
        // 设置下载的图片是否缓存在内存中 
        .cacheInMemory(false) 
        // 设置下载的图片是否缓存在SD卡中 
        .cacheOnDisc(true) 
        // 保留Exif信息 
        .considerExifParams(true) 
        // 设置图片以如何的编码方式显示 
        .imageScaleType(ImageScaleType.EXACTLY_STRETCHED) 
        // 设置图片的解码类型 
        .bitmapConfig(Bitmap.Config.RGB_565) 
        // .decodingOptions(android.graphics.BitmapFactory.Options 
        // decodingOptions)//设置图片的解码配置 
        .considerExifParams(true) 
        // 设置图片下载前的延迟 
        .delayBeforeLoading(100)// int 
        // delayInMillis为你设置的延迟时间 
        // 设置图片加入缓存前,对bitmap进行设置 
        // .preProcessor(BitmapProcessor preProcessor) 
        .resetViewBeforeLoading(true)// 设置图片在下载前是否重置,复位 
        // .displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角,弧度为多少 
        .displayer(new FadeInBitmapDisplayer(100))// 淡入 
        .build(); 
    return options; 
  } 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多