android中使用universalimageloader异步加载图片
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2015-07-16 12:13:58
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
universalimageloader是个开源项目可以在github上取得universalimageloader
先看下这个库的功能
多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等
支持随意的配置ImageLoader,例如线程池,图片下载器,内存缓存策略,硬盘缓存策略,图片显示选项以及其他的一些配置
支持图片的内存缓存,文件系统缓存或者SD卡缓存
支持图片下载过程的监听
根据控件(ImageView)的大小对Bitmap进行裁剪,减少Bitmap占用过多的内存
较好的控制图片的加载过程,例如暂停图片加载,重新开始加载图片,一般使用在ListView,GridView中,滑动过程中暂停加载图片,停止滑动的时候去加载图片
提供在较慢的网络下对图片进行加载
下面是使用方法
首先添加相应权限
配置Android Manifest文件
<manifest> <uses-permission android:name="android.permission.INTERNET" /> <!-- Include next permission if you want to allow UIL to cache images on SD card --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ... <application android:name="MyApplication"> ... </application> </manifest>
下面是对universalimageloader的全局初始化(只用一次就可以啦)
package com.demo.core;
import java.io.File;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import android.app.Application;
import android.content.Context;
public class MYApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
initImageLoader(getApplicationContext());
}
public static void initImageLoader(Context context) {
File cacheDir = StorageUtils.getCacheDirectory(context);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.diskCacheExtraOptions(480, 800, null)
.taskExecutor(null)
.taskExecutorForCachedImages(null)
.threadPoolSize(3) // default
.threadPriority(Thread.NORM_PRIORITY - 2) // default
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.memoryCacheSizePercentage(13) // default
.diskCache(new UnlimitedDiskCache(cacheDir)) // default
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileCount(100)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.imageDownloader(new BaseImageDownloader(context)) // default
.imageDecoder(new BaseImageDecoder(false)) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs()
.build();
ImageLoader.getInstance().init(config);//全局初始化此配置
}
}写一个自己的MYApplication类,再设置为app默认启动的类
<application android:allowBackup="true" android:icon="@drawable/icon3" android:label="@string/app_name" android:name="com.demo.core.MYApplication"<!-- 启动时的类名 --> android:theme="@android:style/Theme.NoTitleBar"> </application>
准备工作已经完成下面就是使用啦
第一种方法
//返回一个实例 ImageLoader imageLoader = ImageLoader.getInstance(); //显示图片参数分别是图片的url地址 和imageview控件对象 imageLoader.displayImage(urlStr,image);
第二种方法使用一些自己的配置如下
DisplayImageOptions options = new DisplayImageOptions.Builder() //.showImageOnLoading(R.drawable.default_ico) // 设置图片在下载期间显示的图片 //.showImageForEmptyUri(R.drawable.default_ico)// 设置图片Uri为空或是错误的时候显示的图片 // .showImageOnFail(R.drawable.default_ico)// 设置图片加载/解码过程中错误时候显示的图片 .resetViewBeforeLoading(true) // default .delayBeforeLoading(1000) .cacheInMemory(true)//设置下载的图片是否缓存在内存中 .cacheOnDisk(true) //设置下载的图片是否缓存在SD卡中 // .postProcessor(null)//在图片显示之前的操作 //.preProcessor(null) //设置图片加入缓存前,对bitmap进行设置 .extraForDownloader(null) .considerExifParams(false) // default .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default .bitmapConfig(Bitmap.Config.ARGB_8888) // default // .decodingOptions(null) .displayer(new SimpleBitmapDisplayer()) // default .displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角,弧度为多少 .displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间 .handler(new Handler()) // default .build();
然后再显示图片使用方法如下
imageLoader.displayImage(urlStr, image, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
Log.v("catch",view+"");
try{
if (loadedImage != null && view!=null) {
ImageView imageView = (ImageView) view;
// imageView.setImageBitmap(loadedImage);
// 是否第一次显示
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
// 图片淡入效果
FadeInBitmapDisplayer.animate(imageView, 800);
displayedImages.add(imageUri);
}
}
}catch(Exception e){
Log.v("catch1",e.getMessage());
}
}
});注意上面有一个图片淡入效果如果要使用的话要添加下面代码
把下面代码添加到你的适配器成员变量里,或是你的当前使用的类里面 static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
ok啦结束,下面是使用的库直接放入libs中就可以
universalimageloader-1.9.4.rar