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

转载于:https://www.cnblogs.com/devli/p/5253323.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值