import java.io.File;
import android.app.Application;
import android.os.Environment;
import com.nostra13.universalimageloader.cache.disc.impl.LimitedAgeDiskCache;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
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.download.BaseImageDownloader;
public class MyApplication extends Application {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
File cacheDir = null;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
cacheDir = new File(Environment.getExternalStorageDirectory(),
"imageloader/Cache");
}
@SuppressWarnings("deprecation")
ImageLoaderConfiguration fig = new ImageLoaderConfiguration.Builder(
this)
.memoryCacheExtraOptions(480, 800)
// maxwidth, max height,即保存的每个缓存文件的最大长宽
.threadPoolSize(3)
// 线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
// You can pass your own memory cache
// implementation/你可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024)
.discCacheSize(50 * 1024 * 1024)
// .discCacheFileNameGenerator(new
// Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密
.tasksProcessingOrder(QueueProcessingType.LIFO)
.discCacheFileCount(100)
// 缓存的文件数量
.discCache(new LimitedAgeDiskCache(cacheDir, 70))
// 自定义缓存路径
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.imageDownloader(
new BaseImageDownloader(this, 5 * 1000, 30 * 1000))
// connectTimeout (5s),readTimeout(30s)超时时间
.writeDebugLogs() // Remove for releaseapp
.build();// 开始构建
ImageLoader.getInstance().init(fig);
}
}
package com.baidu.test.util;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import com.baidu.test.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
public abstract class ImageUtil {
public static DisplayImageOptions getOptions() {
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) // 启用EXIF和JPEG图像格式
.displayer(new Displayer(0)) // 设置圆形
// .displayer(new RoundedBitmapDisplayer(20)) 设置显示风格这里是圆角矩形
.build();
return options;
}
// 设置圆形
static class Displayer extends RoundedBitmapDisplayer {
public Displayer(int cornerRadiusPixels) {
super(cornerRadiusPixels);
}
// 显示位图
@Override
public void display(Bitmap bitmap, ImageAware imageAware,
LoadedFrom loadedFrom) {
imageAware.setImageDrawable(new CircleDrawable(bitmap, margin));
}
class CircleDrawable extends Drawable {
private final int margin;
private final RectF mRect = new RectF();
private final BitmapShader bitmapShader;
private final Paint paint;
private RectF mBitmapRect;
public CircleDrawable(Bitmap bitmap, int margin) {
this.margin = 0;
// 创建着色器
bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
mBitmapRect = new RectF(margin, margin, bitmap.getWidth()
- margin, bitmap.getHeight() - margin);
// 设置画笔
paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(bitmapShader);
}
// 画圆,覆盖原来的位图
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
mRect.set(margin, margin, bounds.width() - margin,
bounds.height() - margin);
// 调整位图,设置该矩阵,转换映射源矩形和目的矩形
Matrix shaderMatrix = new Matrix();
shaderMatrix.setRectToRect(mBitmapRect, mRect,
Matrix.ScaleToFit.FILL);
// 设置着色器矩阵
bitmapShader.setLocalMatrix(shaderMatrix);
}
// 画出其边界(通过设置的setBounds)
@Override
public void draw(Canvas canvas) {
canvas.drawRoundRect(mRect, mRect.width() / 2,
mRect.height() / 2, paint);
}
/**
* 返回此绘制对象的不透明度/透明度 ,返回的值是抽象的格式常数的PixelFormat之一: 未知,半透明,透明或不透明
*/
@Override
public int getOpacity() {
// 半透明
return PixelFormat.TRANSLUCENT;
}
// 设置透明度
@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}
// 彩色滤光片(通过设置setColorFilter)
@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}
}
}
}
//设置图片
ImageLoader.getInstance().displayImage(list.get(position), holder.img, ImageUtil.getOptions());
// 清除本地缓存
ImageLoader.getInstance().clearDiskCache();
// 清除内存缓存
ImageLoader.getInstance().clearMemoryCache();