Android-Universal-Image-Loader应用详解

来源:互联网 发布:node.js web开发 编辑:程序博客网 时间:2024/05/06 00:16

 一、介绍

 Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。所以,如果你的程序里需要这个功能的话,那么不妨试试它。因为已经封装好了一些类和方法。我们 可以直接拿来用了。而不用重复去写了。其实,写一个这方面的程序还是比较麻烦的,要考虑多线程,缓存,内存溢出等很多方面。但是,你也可以参考这个例子来自己写出更好的程序。在此为大家介绍一下:


二、特点
多线程的图像加载
的可能性的宽调谐对ImageLoader的配置(线程池的大小,HTTP选项,内存和光盘高速缓存,显示图像,以及其他)
的图像的可能性中的缓存存储器和/或设备的文件器系统(或SD卡)
可以“听”加载过程中
可自定义每个显示的图像调用分隔的选项
Widget支持
Android 1.5以上支持

三、使用方法

  这是 一个开源的Android关于下载显示图片的工具类,在这个下载包里面偶jar文件,用于我们导入项目使用,具体使用方法在包里面也含有。下面是一个例子:

ImageView imageView = ...String imageUrl = "http://site.com/image.png"; // or "file:///mnt/sdcard/images/image.jpg"ProgressBar spinner = ...File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "UniversalImageLoader/Cache");// Get singletone instance of ImageLoaderImageLoader imageLoader = ImageLoader.getInstance();// Create configuration for ImageLoader (all options are optional, use only those you really want to customize)ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())            .memoryCacheExtraOptions(480, 800) // max width, max height            .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75) // Can slow ImageLoader, use it carefully (Better don't use it)            .threadPoolSize(3)            .threadPriority(Thread.NORM_PRIORITY - 1)            .denyCacheImageMultipleSizesInMemory()            .offOutOfMemoryHandling()            .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation            .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation            .discCacheFileNameGenerator(new HashCodeFileNameGenerator())            .imageDownloader(new URLConnectionImageDownloader(5 * 1000, 20 * 1000)) // connectTimeout (5 s), readTimeout (20 s)            .defaultDisplayImageOptions(DisplayImageOptions.createSimple())            .enableLogging()            .build();// Initialize ImageLoader with created configuration. Do it once on Application start.imageLoader.init(config);
// Creates display image options for custom display task (all options are optional)DisplayImageOptions options = new DisplayImageOptions.Builder()           .showStubImage(R.drawable.stub_image)           .showImageForEmptyUri(R.drawable.image_for_empty_url)           .cacheInMemory()           .cacheOnDisc()           .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)           .displayer(new RoundedBitmapDisplayer(20))           .build();// Load and display imageimageLoader.displayImage(imageUrl, imageView, options, new ImageLoadingListener() {    @Override    public void onLoadingStarted() {       spinner.show();    }    @Override    public void onLoadingFailed(FailReason failReason) {        spinner.hide();    }    @Override    public void onLoadingComplete(Bitmap loadedImage) {        spinner.hide();    }    @Override    public void onLoadingCancelled() {        // Do nothing    }});
// Just load imageDisplayImageOptions options = new DisplayImageOptions.Builder()           .cacheInMemory()           .cacheOnDisc()           .imageScaleType(ImageScaleType.IN_SAMPLE_INT)           .displayer(new FakeBitmapDisplayer())           .build();ImageSize minImageSize = new ImageSize(120, 80);imageLoader.loadImage(context, imageUrl, minImageSize, options, new SimpleImageLoadingListener() {    @Override    public void onLoadingComplete(Bitmap loadedImage) {        // Do whatever you want with loaded Bitmap    }});
四、使用信息

缓存不是默认启用如果你想将被缓存在内存中加载图像和/或那么你应该启用缓存displayimageoptions方式如下

// Create default options which will be used for every //  displayImage(...) call if no options will be passed to this methodDisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()        ...        .cacheInMemory()        .cacheOnDisc()        ...        .build();ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())        ...        .defaultDisplayImageOptions(defaultOptions)        ...        .build();ImageLoader.getInstance().init(config); // Do it on Application start
// Then later, when you want to display imageImageLoader.getInstance().displayImage(imageUrl, imageView); // Default options will be used
使用Android-Universal-Image-Loader的例子程序:
  • MediaHouse, UPnP/DLNA Browser
  • Деловой Киров
  • Бизнес-завтрак
  • Menu55
  • SpokenPic
  • Kumir
  • EUKO 2012
  • TuuSo Image Search
  • Газета Стройка
  • Prezzi Benzina (AndroidFuel)
  • Quiz Guess The Guy
  • Volksempfänger (alpha)
  • ROM Toolbox Lite | Pro
  • London 2012 Games
  • 카톡 이미지 - 예쁜 프로필 이미지
  • dailyPen
  • TK App
  • Mania!
  • Stadium Astro
  • Chef Astro
  • Lafemme Fashion Finder
  • FastPaleo
  • Live Soccer Scores
  • friendizer
  • LowPrice lowest book price
  • bluebee
  • Game PromoBox
  • EyeEm - Photo Filter Camera
  • Festival Wallpaper
  • Gaudi Hall
  • Spocal
  • PhotoDownloader for Facebook 

大家 可以好好看看这些例子,他们是如何把这个组件应用到程序里面的。这个开源程序的下载地址是:https://nodeload.github.com/nostra13/Android-Universal-Image-Loader/zipball/master

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 预约成功后驾校不提交档案怎么办 c2刚满一年的驾照扣12分怎么办 怀孕6个月咳嗽很厉害怎么办 家门口有电线杆影响我建楼房怎么办 卡丢了不知道卡号怎么办 驾考网上预约用户被锁定了怎么办 人才中心拿出来的户口掉了怎么办 父母是南京集体户孩子没户口怎么办 二建挂靠中介单位不给证怎么办 小包工头遇到工人在工地摔伤怎么办 外地人买了城中村的房子改造怎么办 深圳社保怀孕了产检异地怎么办 成都公租房租满6年怎么办 二建审核资料如果照片丢失怎么办 身份证被冒用在外地办社保怎么办 蔷薇的嫩叶都被太阳晒死了怎么办 乐视手机进水了屏幕失灵怎么办 乐视手机进水了屏幕不显示怎么办 美团商家单量出现下滑怎么办 想开个小超市没经营过怎么办 华为7c手机wifi信号差怎么办 贞子从电视里爬出来怎么办 2个月的婴儿吓到怎么办 排卵日同房了没避孕怕怀孕怎么办 妻子因为我欺骗她要跟我离婚怎么办 老婆用苹果手机共享我的位置怎么办 孕妇餐后2小时血糖偏高怎么办 孕妇血糖餐后2小时数值高怎么办 股市退市的话股民的钱怎么办 美股股票退市了手里的股票怎么办 坐高铁安检时怕把包包弄坏了怎么办 很贵的包包高铁安检怎么办 如果过高铁安检东西被扣留怎么办 邻居家小孩把我家东西弄坏了怎么办 邻居早上6点放音乐扰民怎么办 隔壁楼邻居天天放音乐很吵怎么办 发现老公在卧室安了摄像头怎么办 憋的尿又没有厕所的时候怎么办 一岁宝宝拉屎总是拉出血怎么办 孩子鼻窦炎鼻子堵有白色鼻涕怎么办 家里阳台上老是有蝙蝠趴着怎么办