ImageLoader使用

最近琢磨的网上的这些开源框架,imageLoader确实很好用,基本用法不记录了,参考网上的一些代码写了个ImageLoaderManager

 1 /**
 2  * Created by Lee on 2016/2/22.
 3  */
 4 public class ImageLoaderManager {
 5     public static DisplayImageOptions.Builder IMAGE_OPTION_DEFAULT = new DisplayImageOptions.Builder()
 6             .cacheInMemory(true).cacheOnDisk(true).displayer(new SimpleBitmapDisplayer())
 7             .resetViewBeforeLoading(true).considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565);
 8 
 9     public static DisplayImageOptions IMAGE_OPTION_DEFAULT_PIC = IMAGE_OPTION_DEFAULT
10             .showImageForEmptyUri(R.drawable.ic_launcher)
11             .showImageOnFail(R.drawable.ic_launcher)
12             .showImageOnLoading(R.drawable.ic_launcher)
13             .build();
14 
15     public static void init(Context context){
16         ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).
17                 memoryCacheExtraOptions(480, 800).   //max width, max height
18                 threadPoolSize(3).                    //线程池内加载的数量
19                 threadPriority(Thread.NORM_PRIORITY).denyCacheImageMultipleSizesInMemory().
20                 memoryCache(new LruMemoryCache(2 * 1024 * 1024)).   //you can pass your own memory cache implementation
21                 memoryCacheSize(2 * 1024 * 1024).
22                 diskCacheSize(50 * 1024 * 1024)
23                 .diskCacheFileNameGenerator(new Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密
24                 .tasksProcessingOrder(QueueProcessingType.LIFO)
25                 .diskCacheFileCount(100) //缓存的文件数量
26                 .diskCache(new UnlimitedDiskCache(StorageUtils.getCacheDirectory(context)))//自定义缓存路径
27                 .defaultDisplayImageOptions(IMAGE_OPTION_DEFAULT.build())
28                 .imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
29                 .build();//开始构建
30         ImageLoader.getInstance().init(config);//全局初始化此配置
31     }
32 }

我们需要在全局初始化这个ImageLoader,所以在application类中我们需要init

 1 public class MyApplication extends Application{
 2 
 3     private static MyApplication instance;
 4 
 5     public static MyApplication getInstance(){
 6         return instance;
 7     }
 8 
 9     @Override
10     public void onCreate() {
11         super.onCreate();
12 
13         instance = this;
14         initImageLoader();
15     }
16 
17     private static void initImageLoader(){
18         ImageLoaderManager.init(instance.getApplicationContext());
19     }

调用时只需要一行代码

    public void setImage(String url){
        ImageLoader.getInstance().displayImage(url, image, ImageLoaderManager.IMAGE_OPTION_DEFAULT_PIC);
    }

image为imageview

posted @ 2016-03-08 11:22  DevLi  阅读(656)  评论(0编辑  收藏  举报