当前位置: 移动技术网 > 移动技术>移动开发>Android > Android Imageloader的配置的实现代码

Android Imageloader的配置的实现代码

2019年07月24日  | 移动技术网移动技术  | 我要评论

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; 
  } 
} 

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网