Android LruCache图片异步加载工具类

使用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();
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值