使用LruCache进行图片异步缓存加载
AsyncImageLoader2.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
/**
* 图片异步加载工具类
*
* @version V2.0
*/
public class AsyncImageLoader2 {
private LruCache<String, SoftReference<Bitmap>> imageCache = new LruCache<String, SoftReference<Bitmap>>(
100);
private static ExecutorService executorService = Executors
.newFixedThreadPool(10);
public AsyncImageLoader2() {
imageCache = new LruCache<String, SoftReference<Bitmap>>(100);
}
public AsyncImageLoader2(int maxSize) {
imageCache = new LruCache<String, SoftReference<Bitmap>>(maxSize);
}
public Bitmap loadImage(final String imageUrl,
final ImageCallback imageCallback, final boolean isZoom) {
SoftReference<Bitmap> softReference = imageCache.get(imageUrl);
if (softReference != null) {
Bitmap bitmap = softReference.get();
if (bitmap != null && !bitmap.isRecycled()) {
return bitmap;
}
}
final Handler handler = new Handler() {
public void handleMessage(Message message) {
imageCallback.imageLoaded((Bitmap) message.obj, imageUrl);
}
};
executorService.submit(new Runnable() {
@Override
public void run() {
Bitmap bitmap = null;
if (imageUrl.startsWith("http")) {
bitmap = loadImageFromNet(imageUrl, isZoom);
} else {
bitmap = loadImageFromLocal(imageUrl, isZoom);
}
if (bitmap != null) {
imageCache.put(imageUrl, new SoftReference<Bitmap>(bitmap));
Message message = handler.obtainMessage(0, bitmap);
handler.sendMessage(message);
}
}
});
return null;
}
public interface ImageCallback {
public void imageLoaded(Bitmap bitmap, String imageUrl);
}
private Bitmap loadImageFromLocal(String path, boolean isZoom) {
Bitmap bitmap = null;
try {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.RGB_565;
if (isZoom) {
opts.inSampleSize = 2;
}
opts.inPurgeable = true;
opts.inInputShareable = true;
InputStream is = new FileInputStream(new File(path));
bitmap = BitmapFactory.decodeStream(is, null, opts);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
private Bitmap loadImageFromNet(String imageUrl, boolean isZoom) {
Bitmap bitmap = null;
try {
String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1);
bitmap = loadImageFromLocal(FileUtils.SDCARD_BACKUP_IMAGE
+ fileName, isZoom);
if (bitmap == null) {
URL url = new URL(imageUrl);
InputStream is = url.openStream();
FileUtils.saveImage(is, fileName);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = false;
if (isZoom) {
opts.inSampleSize = 2;
}
opts.inPurgeable = true;
opts.inInputShareable = true;
bitmap = BitmapFactory.decodeStream(is, null, opts);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bitmap;
}
public void removeFromContainer(String key) {
SoftReference<Bitmap> softReference = imageCache.get(key);
if (softReference != null) {
Bitmap bitmap = softReference.get();
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
softReference.clear();
}
}
}
}