Android图片异步加载框架Android-Universal-Image-Loader
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android-Universal-Image-Loader是一个图片异步加载,缓存和显示的框架。这个框架已经被很多开发者所使用,是最常用的几个 Android开源项目之一,主流的应用,随便反编译几个,都可以见到它的身影。淘宝,天猫,Facebook,京东商城等都用到了这个项目。
该项目的Github地址链接:https://github.com/nostra13/Android-Universal-Image-Loader
运行流程:
每一个图片的加载和显示任务都运行在独立的线程中,除非这个图片缓存在内存中,这种情况下图片会立即显示。如果需要的图片缓存在本地,它会开启一个独立的线程队列。如果在缓存中没有正确的图片,任务线程会从网络中获取。
使用步骤:
1、加载图片之前,先要做初始化配置,这个类似很多游戏引擎使用前要做一下初始化,其实只做了一件事,实例化一个全局的ImageLoader对象,同时
传入图片加载缓存的配置,ImageLoaderConfiguration封装了基本的配置信息,比如加载图片事用的线程池大小,线程的优先级,内存缓
存大小,是否支持同一图片的多尺寸缓存(默认是支持的,可以手动关闭),还有缓存的命名规则等等。
2、配置完后,就可以开始使用了,通过ImageLoader的displayImage()绑定一个图片和ImageView。
displayImage(String uri, ImageView imageView, DisplayImageOptions options)
DisplayImageOptions用于指导每一个Imageloader根据网络图片的状态(空白、下载错误、正在下载)显示对应的图片,是否将缓存加载到磁盘上,下载完后对图片进行怎么样的处理。
实例:UniversalImageLoaderDemo
运行效果:
代码清单:
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.rainsong.universalimageloaderdemo"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="19" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.INTERNET" />
- <application
- android:name="com.rainsong.universalimageloaderdemo.UILApplication"
- android:allowBackup="true"
- android:icon="@drawable/logo"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.rainsong.universalimageloaderdemo.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
布局文件:activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <ListView
- android:id="@+id/list"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- </ListView>
- </LinearLayout>
布局文件:item_list.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <ImageView
- android:id="@+id/image"
- android:layout_width="72dip"
- android:layout_height="72dip"
- android:layout_margin="3dip"
- android:adjustViewBounds="true"
- android:contentDescription="@string/descr_image"
- android:scaleType="centerCrop" />
- <TextView
- android:id="@+id/text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="start|center_vertical"
- android:layout_marginLeft="20dip"
- android:textSize="22sp" />
- </LinearLayout>
Java源代码文件:UILApplication.java
- package com.rainsong.universalimageloaderdemo;
- import java.io.File;
- import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
- import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
- import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
- import com.nostra13.universalimageloader.core.ImageLoader;
- import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
- import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
- import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
- import com.nostra13.universalimageloader.utils.StorageUtils;
- import android.app.Application;
- import android.content.Context;
- public class UILApplication 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);
- }
- }
Java源代码文件:MainActivity.java
- package com.rainsong.universalimageloaderdemo;
- import com.nostra13.universalimageloader.core.DisplayImageOptions;
- import com.nostra13.universalimageloader.core.ImageLoader;
- import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- 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;
- }
- }
- }
http://blog.csdn.net/hantangsongming/article/details/41961749
Android图片异步加载框架Android-Universal-Image-Loader的更多相关文章
- Android 图片异步加载的体会,SoftReference已经不再适用
在网络上搜索Android图片异步加载的相关文章,目前大部分提到的解决方案,都是采用Map<String, SoftReference<Drawable>> 这样软引用的 ...
- Android图片异步加载之Android-Universal-Image-Loader
将近一个月没有更新博客了,由于这段时间以来准备毕业论文等各种事务缠身,一直没有时间和精力沉下来继续学习和整理一些东西.最近刚刚恢复到正轨,正好这两天看了下Android上关于图片异步加载的开源项目,就 ...
- Android图片异步加载之Android-Universal-Image-Loader(转)
今天要介绍的是Github上一个使用非常广泛的图片异步加载库Android-Universal-Image-Loader,该项目的功能十分强大,可以说是我见过的目前功能最全.性能最优的图片异步加载解决 ...
- [置顶] Android图片异步加载之Android-Universal-Image-Loader
将近一个月没有更新博客了,由于这段时间以来准备毕业论文等各种事务缠身,一直没有时间和精力沉下来继续学习和整理一些东西.最近刚刚恢复到正轨,正好这两天看了下Android上关于图片异步加载的开源项目,就 ...
- Android图片异步加载
原:http://www.cnblogs.com/angeldevil/archive/2012/09/16/2687174.html 相关:https://github.com/nostra13/A ...
- Android图片异步加载的方法
很多时候,我们在加载大图片或者需要处理较多图像数据的时候,希望显示效果能好点,不至于因为图片解码耗时产生ANR等情况,不得不说异步加载是个不错的方法.说到异步加载,避免application出现ANR ...
- Android 图片异步加载 加载网络图片
最近用到了加载网络图片,研究了一下,写一点简单的介绍: 首先创建一个线程去取图片(网络请求必须放在线程中): /** * 使用继承java.lang.Thread类的方式创建一个线程 * 直接取图片, ...
- android 异步加载框架 原理完全解析
一.手写异步加载框架MyAsycnTask(核心原理) 1.我为大家手写了一个异步加载框架,涵盖了异步加载框架核心原理. MyAsycnTask.java import android.os.Hand ...
- Android ListView 图片异步加载和图片内存缓存
开发Android应用经常需要处理图片的加载问题.因为图片一般都是存放在服务器端,需要联网去加载,而这又是一个比较耗时的过程,所以Android中都是通过开启一个异步线程去加载.为了增加用户体验,给用 ...
随机推荐
- Emacs学习心得之 基础配置
作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Emacs学习心得之 基础配置 1.前言2.基础配置 一.前言 本篇博文记录了Emacs的一 ...
- Java File类总结和FileUtils类
Java File类总结和FileUtils类 文件存在和类型判断 创建出File类的对象并不代表该路径下有此文件或目录. 用public boolean exists()可以判断文件是否存在. Fi ...
- git 上的pull request 是什么意思?
1.git 上有常见的pull request 功能 2.pull request 的含义 解释一: 有一个仓库,叫Repo A.你如果要往里贡献代码,首先要Fork这个Repo,于是在你的Gi ...
- swift 2.2 语法 (中)
前言: 1.此文中的语法会根据Swift的升级变动而更新. 2.如果需要请移步 -> swift2.2 语法(上).swift 2.2语法(下) 函数 和C语言一样,swift也有函数,性质和我 ...
- 【代码笔记】iOS-带输入框的UIAlertView
一,效果图. 二,代码. //点击任何处,弹出输入框 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UIAlertV ...
- 基于ruby的watir自动化测试 笔记一
基于Ruby的watir-webdriver自动化测试方案与实施(五) 基于Ruby的watir-webdriver自动化测试方案与实施(四) 基于Ruby的watir-webdriver自动 ...
- 【转】JavaScript 异步进化史
前言 JS 中最基础的异步调用方式是 callback,它将回调函数 callback 传给异步 API,由浏览器或 Node 在异步完成后,通知 JS 引擎调用 callback.对于简单的异步操作 ...
- mac 远程连接服务器
很多刚用mac的同学 可能会纠结,连接远程服务器咋整? 有没有类型windows上的securecrt 其实,完全可以不用: mac自带的终端就可以搞定:终端terminal 如何连接远程服务器? s ...
- Java调用webservice接口方法
java调用webservice接口 webservice的 发布一般都是使用WSDL(web service descriptive langu ...
- dotNet使用HttpWebRequest模拟浏览器
在编写网络爬虫时,HttpWebRequest几乎可以完成绝大多数网站的抓取,为了更好的使用这一技术,我将常用的几个功能进行了封装,以方便调用.这个类已经在多个项目中得到使用,主要解决了Cookies ...