android universal-image-loader的使用
- 全局配置 android-image-loader的使用
- public class Application extends Application {
- @Override
- public void onCreate() {
- super.onCreate();
- initImageLoader(getApplicationContext());
- }
- public static void initImageLoader(Context context) {
- //缓存文件的目录
- File cacheDir = StorageUtils.getOwnCacheDirectory(context, "universalimageloader/Cache");
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
- .memoryCacheExtraOptions(480, 800) // max width, max height,即保存的每个缓存文件的最大长宽
- .threadPoolSize(3) //线程池内线程的数量
- .threadPriority(Thread.NORM_PRIORITY - 2)
- .denyCacheImageMultipleSizesInMemory()
- .diskCacheFileNameGenerator(new Md5FileNameGenerator()) //将保存的时候的URI名称用MD5 加密
- .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
- .memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
- .diskCacheSize(50 * 1024 * 1024) // SD卡缓存的最大值
- .tasksProcessingOrder(QueueProcessingType.LIFO)
- // 由原先的discCache -> diskCache
- .diskCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径
- .imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
- .writeDebugLogs() // Remove for release app
- .build();
- //全局初始化此配置
- ImageLoader.getInstance().init(config);
- }
- }
主要类文件
- public class MainActivity extends Activity {
- private ImageLoader imageLoader;
- private ListView lv;
- private String[] imageUrls;
- private DisplayImageOptions options;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imageLoader = ImageLoader.getInstance();
- lv = (ListView)findViewById(R.id.list);
- imageUrls = Constants.images;
- // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
- options = new DisplayImageOptions.Builder()
- .showImageOnLoading(R.drawable.ic_stub) // 设置图片下载期间显示的图片
- .showImageForEmptyUri(R.drawable.ic_empty) // 设置图片Uri为空或是错误的时候显示的图片
- .showImageOnFail(R.drawable.ic_error) // 设置图片加载或解码过程中发生错误显示的图片
- .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
- .cacheOnDisk(true) // 设置下载的图片是否缓存在SD卡中
- .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片
- .build(); // 构建完成
- lv.setAdapter(new ItemListAdapter());
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.item_clear_memory_cache:
- ImageLoader.getInstance().clearMemoryCache();
- return true;
- case R.id.item_clear_disc_cache:
- ImageLoader.getInstance().clearDiskCache();
- return true;
- default:
- return false;
- }
- }
- class ItemListAdapter extends BaseAdapter {
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return imageUrls.length;
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return imageUrls[position];
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- ViewHolder holder = null;
- if (convertView == null) {
- convertView = getLayoutInflater().inflate(R.layout.item_list, parent, false);
- holder = new ViewHolder();
- holder.text = (TextView) convertView.findViewById(R.id.text);
- holder.image = (ImageView) convertView.findViewById(R.id.image);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- holder.text.setText("Item " + (position + 1));
- imageLoader.displayImage(imageUrls[position], holder.image, options);
- return convertView;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- class ViewHolder {
- public ImageView image;
- public TextView text;
- }
- }
- }
android universal-image-loader的使用的更多相关文章
- android universal image loader 缓冲原理详解
1. 功能介绍 1.1 Android Universal Image Loader Android Universal Image Loader 是一个强大的.可高度定制的图片缓存,本文简称为UIL ...
- Android Universal Image Loader java.io.FileNotFoundException: http:/xxx/lxx/xxxx.jpg
前段时间在使用ImageLoader异步加载服务端返回的图片时总是出现 java.io.FileNotFoundException: http://xxxx/l046/10046137034b1c0d ...
- Android中Universal Image Loader开源框架的简单使用
UIL (Universal Image Loader)aims to provide a powerful, flexible and highly customizable instrument ...
- 【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )
作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50824912 相关地址介绍 : -- Universal I ...
- 开源项目Universal Image Loader for Android 说明文档 (1) 简介
When developing applications for Android, one often facesthe problem of displaying some graphical ...
- 开源项目Universal Image Loader for Android 说明文档 (1) 简单介绍
When developing applications for Android, one often facesthe problem of displaying some graphical ...
- universal image loader在listview/gridview中滚动时重复加载图片的问题及解决方法
在listview/gridview中使用UIL来display每个item的图片,当图片数量较多需要滑动滚动时会出现卡顿,而且加载过的图片再次上翻后依然会重复加载(显示设置好的加载中图片) 最近在使 ...
- eclipse android sdk content loader一直显示0%的问题解决
今天上班启动eclipse,发现eclipse 一直卡在android sdk content loader的地方,一直显示为0%.百度后发现很多都是一下解决方法: 关闭Eclipse,删掉Ecli ...
- Android SDK content Loader has encountered a problem.parseSdkContent failed
打开Eclipse,弹出Android SDK content Loader has encountered a problem.parseSdkContent failed,当点击detail按钮, ...
- universal image loader自己使用的一些感受
1.全局入口的Application定义初始化: ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Build ...
随机推荐
- Topshelf 支持Mono 扩展Topshelf.Linux
使用Topshelf 5步创建Windows 服务 这篇文章大家可以了解到使用Topshelf可以很好的支持Windows服务的开发,但是它和Mono不兼容,Github上有一个扩展https://g ...
- 集成基于OAuth协议的单点登陆
在之前的一篇文章中,我们已经介绍了如何为一个应用添加对CAS协议的支持,进而使得我们的应用可以与所有基于CAS协议的单点登陆服务通讯.但是现在的单点登陆服务实际上并不全是通过实现CAS协议来完成的.例 ...
- C++的性能C#的产能?! - .Net Native 系列四:性能测试方法(PerfView)
之前一文<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATIVE初窥> 获得很多朋友支持和鼓励,也更让我坚定做这项技术的推广者,希望能让更多的朋友了解这项技术,于是先从官方 ...
- 解决CSharpGL使用CGCompiler时发现的几个问题
解决CSharpGL使用CGCompiler时发现的几个问题 为了获取CSharpShadingLanguage的token流,我设计了这样一个文法: <Expression> ::= & ...
- LeetCode-2AddTwoNumbers(C#)
# 题目 2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The di ...
- C# 仿刷-框架MvcThrottle的使用
1.介绍 1)用MvcThrottle你能保护你的网站不受攻击.刷. 2)你可以限制与设置多个不同场景允许的IP,设置 每秒/分/天 允许访问IP. 3)你可以定义限制,来处理所有请求.或者某个Con ...
- Failed to stop iptables.service: Unit iptables.service not loaded.
redhat 7 [root@lk0 ~]# service iptables stop Redirecting to /bin/systemctl stop iptables.service Fai ...
- (转)面向属性的CSS命名
原文链接:戳这里 自从开始做前端开发以来,我发现在开发页面的时候,总是有一个问题十分影响自己的开发效率,这个问题就是css的命名,主要是指css类选择器的命名.这个问题主要体现在:第一,有的内容你压根 ...
- iOS 之消息推送(个推)---个人小结
前言:自从上个星期开始整这个推送,弄了差不多一个星期,今天终于给整好了,因此现在来记录这段"奇妙"的旅程. 我们公司使用的消息推送是用的第三方--个推,这里不得不说一下,个推的技术 ...
- STL标准模板库(简介)
标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...