import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Formatter.BigDecimalLayoutForm;
import android.R.string;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import com.bjzjxd.cdz.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
public class ImageLoaderTool {
/**
* 加载圆形图片
*/
public static void loadCircleImage(String imgUrl, ImageView imageView) {
ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.my_icon_default)// 添加正在加载中的图片
.showImageForEmptyUri(R.drawable.my_icon_default)// 设置当链接为空的图标
.showImageOnFail(R.drawable.my_icon_default).cacheInMemory(true)// 设置缓存
.cacheOnDisk(true).considerExifParams(true)//
.bitmapConfig(Bitmap.Config.RGB_565)//配置bitmapConfig为Bitmap.Config.RGB_565,因为默认是ARGB_8888, 使用RGB_565会比使用ARGB_8888少消耗2倍的内存
.displayer(new RoundedBitmapDisplayer(90)).build();// 90表示圆角
ImageLoader.getInstance().displayImage(imgUrl, imageView, options,
animateFirstListener);
}
/**
* 加载图片,根据自己的角度去加载
*/
public static void loadImageWithAngle(String imgUrl, ImageView imageView,
int angle) {
ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)// 添加正在加载中的图片
.showImageForEmptyUri(R.drawable.ic_launcher)// 设置当链接为空的图标
.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)// 设置缓存
.cacheOnDisk(true).considerExifParams(true)//
.displayer(new RoundedBitmapDisplayer(angle)).build();// 90表示圆角
ImageLoader.getInstance().displayImage(imgUrl, imageView, options,
animateFirstListener);
}
/**
* 加载图片根据自己的定义去加载
*/
public static void loadImageWithDisplayImageOptions(String imgUrl,
ImageView imageView, DisplayImageOptions options) {
ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
ImageLoader.getInstance().displayImage(imgUrl, imageView, options,
animateFirstListener);
}
/**
* 动画加载效果
*
* @author Administrator
*
*/
private static class AnimateFirstDisplayListener extends
SimpleImageLoadingListener {
static final List<string> displayedImages = Collections
.synchronizedList(new LinkedList<string>());
// 图片加载
@Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 2000);// 设置动画渐变时间
//displayedImages.add(imageUri);
}
}
}
}
}
在BaseApplication中的onCreate()方法里添加配置文件
/**
* 异步加载图片全局配置
*/
File cacheDir = StorageUtils.getCacheDirectory(context); //缓存文件夹路径
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
this)
.threadPriority(Thread.NORM_PRIORITY - 2)
.threadPoolSize(3)
.denyCacheImageMultipleSizesInMemory()
.diskCache(new UnlimitedDiscCache(cacheDir)) // default 自定义缓存路径
.build();
ImageLoader.getInstance().init(config);
universal-image-loader-1.9.3.jar下载地址
http://download.csdn.net/detail/u014408122/9565079