1. 全局配置 android-image-loader的使用
  2. public class Application extends Application {
  3. @Override
  4. public void onCreate() {
  5. super.onCreate();
  6. initImageLoader(getApplicationContext());
  7. }
  8. public static void initImageLoader(Context context) {
  9. //缓存文件的目录
  10. File cacheDir = StorageUtils.getOwnCacheDirectory(context, "universalimageloader/Cache");
  11. ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
  12. .memoryCacheExtraOptions(480, 800) // max width, max height,即保存的每个缓存文件的最大长宽
  13. .threadPoolSize(3) //线程池内线程的数量
  14. .threadPriority(Thread.NORM_PRIORITY - 2)
  15. .denyCacheImageMultipleSizesInMemory()
  16. .diskCacheFileNameGenerator(new Md5FileNameGenerator()) //将保存的时候的URI名称用MD5 加密
  17. .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
  18. .memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
  19. .diskCacheSize(50 * 1024 * 1024)  // SD卡缓存的最大值
  20. .tasksProcessingOrder(QueueProcessingType.LIFO)
  21. // 由原先的discCache -> diskCache
  22. .diskCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径
  23. .imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
  24. .writeDebugLogs() // Remove for release app
  25. .build();
  26. //全局初始化此配置
  27. ImageLoader.getInstance().init(config);
  28. }
  29. }

主要类文件

  1. public class MainActivity extends Activity {
  2. private ImageLoader imageLoader;
  3. private ListView lv;
  4. private String[] imageUrls;
  5. private DisplayImageOptions options;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. imageLoader = ImageLoader.getInstance();
  11. lv = (ListView)findViewById(R.id.list);
  12. imageUrls = Constants.images;
  13. // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
  14. options = new DisplayImageOptions.Builder()
  15. .showImageOnLoading(R.drawable.ic_stub) // 设置图片下载期间显示的图片
  16. .showImageForEmptyUri(R.drawable.ic_empty) // 设置图片Uri为空或是错误的时候显示的图片
  17. .showImageOnFail(R.drawable.ic_error) // 设置图片加载或解码过程中发生错误显示的图片
  18. .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
  19. .cacheOnDisk(true) // 设置下载的图片是否缓存在SD卡中
  20. .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片
  21. .build(); // 构建完成
  22. lv.setAdapter(new ItemListAdapter());
  23. }
  24. @Override
  25. public boolean onCreateOptionsMenu(Menu menu) {
  26. // Inflate the menu; this adds items to the action bar if it is present.
  27. getMenuInflater().inflate(R.menu.main, menu);
  28. return true;
  29. }
  30. @Override
  31. public boolean onOptionsItemSelected(MenuItem item) {
  32. switch (item.getItemId()) {
  33. case R.id.item_clear_memory_cache:
  34. ImageLoader.getInstance().clearMemoryCache();
  35. return true;
  36. case R.id.item_clear_disc_cache:
  37. ImageLoader.getInstance().clearDiskCache();
  38. return true;
  39. default:
  40. return false;
  41. }
  42. }
  43. class ItemListAdapter extends BaseAdapter {
  44. @Override
  45. public int getCount() {
  46. // TODO Auto-generated method stub
  47. return imageUrls.length;
  48. }
  49. @Override
  50. public Object getItem(int position) {
  51. // TODO Auto-generated method stub
  52. return imageUrls[position];
  53. }
  54. @Override
  55. public View getView(int position, View convertView, ViewGroup parent) {
  56. // TODO Auto-generated method stub
  57. ViewHolder holder = null;
  58. if (convertView == null) {
  59. convertView = getLayoutInflater().inflate(R.layout.item_list, parent, false);
  60. holder = new ViewHolder();
  61. holder.text = (TextView) convertView.findViewById(R.id.text);
  62. holder.image = (ImageView) convertView.findViewById(R.id.image);
  63. convertView.setTag(holder);
  64. } else {
  65. holder = (ViewHolder) convertView.getTag();
  66. }
  67. holder.text.setText("Item " + (position + 1));
  68. imageLoader.displayImage(imageUrls[position], holder.image, options);
  69. return convertView;
  70. }
  71. @Override
  72. public long getItemId(int position) {
  73. // TODO Auto-generated method stub
  74. return position;
  75. }
  76. class ViewHolder {
  77. public ImageView image;
  78. public TextView text;
  79. }
  80. }
  81. }

android universal-image-loader的使用的更多相关文章

  1. android universal image loader 缓冲原理详解

    1. 功能介绍 1.1 Android Universal Image Loader Android Universal Image Loader 是一个强大的.可高度定制的图片缓存,本文简称为UIL ...

  2. Android Universal Image Loader java.io.FileNotFoundException: http:/xxx/lxx/xxxx.jpg

    前段时间在使用ImageLoader异步加载服务端返回的图片时总是出现 java.io.FileNotFoundException: http://xxxx/l046/10046137034b1c0d ...

  3. Android中Universal Image Loader开源框架的简单使用

    UIL (Universal Image Loader)aims to provide a powerful, flexible and highly customizable instrument ...

  4. 【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )

    作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50824912 相关地址介绍 : -- Universal I ...

  5. 开源项目Universal Image Loader for Android 说明文档 (1) 简介

     When developing applications for Android, one often facesthe problem of displaying some graphical ...

  6. 开源项目Universal Image Loader for Android 说明文档 (1) 简单介绍

     When developing applications for Android, one often facesthe problem of displaying some graphical ...

  7. universal image loader在listview/gridview中滚动时重复加载图片的问题及解决方法

    在listview/gridview中使用UIL来display每个item的图片,当图片数量较多需要滑动滚动时会出现卡顿,而且加载过的图片再次上翻后依然会重复加载(显示设置好的加载中图片) 最近在使 ...

  8. eclipse android sdk content loader一直显示0%的问题解决

    今天上班启动eclipse,发现eclipse 一直卡在android sdk content loader的地方,一直显示为0%.百度后发现很多都是一下解决方法:  关闭Eclipse,删掉Ecli ...

  9. Android SDK content Loader has encountered a problem.parseSdkContent failed

    打开Eclipse,弹出Android SDK content Loader has encountered a problem.parseSdkContent failed,当点击detail按钮, ...

  10. universal image loader自己使用的一些感受

    1.全局入口的Application定义初始化: ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Build ...

随机推荐

  1. centos 7 安装mono 和 monodevelop

    本次所有操作在root模式下 1.执行  rpm --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3 ...

  2. Mono+Jexus让C#运行在Linux(centos7_x64),学习笔记

    .h2cls { background: #6fa833 none repeat scroll 0 0 !important; color: #fff; font-family: "微软雅黑 ...

  3. Fedora 22中的Locale and Keyboard Configuration

    Introduction The system locale specifies the language settings of system services and user interface ...

  4. ABP框架 - 缓存

    文档目录 本节内容: 简介 ICacheManager ICache ITypedCache 配置 实体缓存 EntityCache 是如何工作 Redis 缓存集成 简介 ABP提供了一个缓存接口, ...

  5. Entity Framework 6 Recipes 2nd Edition(10-3)译 -> 返回结果是一个标量值

    10-3. 返回结果是一个标量值 问题 想取得存储过程返回的一个标量值. 解决方案 假设我们有如Figure 10-2所示的ATM机和ATM机取款记录的模型 Figure 10-2. 一个ATM机和A ...

  6. 我为NET狂~群福利:逆天书库

    我为NET狂-官方群① 238575862 爱学习,爱研究,福利不断,技能直彪~~ 最近更新:2016-08-30,欢迎补充 暂缺PDF: │ SQL Server 2012 Analysis Ser ...

  7. ★Kali信息收集★8.Nmap :端口扫描

    ★Kali信息收集~ 0.Httrack 网站复制机 http://www.cnblogs.com/dunitian/p/5061954.html ★Kali信息收集~ 1.Google Hackin ...

  8. jQuery系列:Ajax

    1. load(url, [data], [callback]) 1.1 解析 载入远程 HTML 文件代码并插入至 DOM 中. 语法格式: load(url, [data], [callback] ...

  9. Redis数据结构详解之Zset(五)

    序言 Zset跟Set之间可以有并集运算,因为他们存储的数据字符串集合,不能有一样的成员出现在一个zset中,但是为什么有了set还要有zset呢?zset叫做有序集合,而set是无序的,zset怎么 ...

  10. 验证码识别<1>

    1. 引子 前两天访问学校自助服务器()缴纳网费,登录时发现这系统的验证码也太过“清晰”了,突然脑袋里就蹦出一个想法:如果能够自动识别验证码,然后采用暴力破解的方式,那么密码不是可以轻易被破解吗? p ...