public class MyLoader extends Application{
@Override
public void onCreate() {
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)
.threadPoolSize(3)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.discCacheSize(50 * 1024 * 1024)
.tasksProcessingOrder(QueueProcessingType.LIFO)
.discCacheFileCount(100)
.discCache(new LimitedAgeDiskCache(cacheDir, 70))
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.imageDownloader(
new BaseImageDownloader(this, 5 * 1000, 30 * 1000))
.writeDebugLogs()
.build();
ImageLoader.getInstance().init(fig);
}
}
public 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)
.displayer(new Displayer(0))
.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);
}
@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);
}
@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}
}
}
}
//加载图片
ImageLoader.getInstance().displayImage("URL", holder.iv, ImageUtil.getOptions())
// 清除本地缓存
ImageLoader.getInstance().clearDiskCache()
// 清除内存缓存
ImageLoader.getInstance().clearMemoryCache()