**
android图片加载—二级缓存
最近闲着没事然后突然想体验一把开源的感觉就寻思着写一个图片加载的框架,然后查阅了一些资料开始动手了。
因为要做双缓存,一个内存缓存和一个磁盘缓存,结合设计模式的思想我就先抽象出一个接口来。
public interface ImageCache {
void put(String url, Bitmap bit);
Bitmap get(String url);
}
这个接口有put 和 get 两个方法分别是加入缓存和从缓存中取出,然后我们写一个缓存类来实现这个接口
public class ImlCache implements ImageCache {
//内存缓存
private LruCache<String, Bitmap> mLruCache;
//磁盘缓存
private DiskLruCache mDiskLruCache;
private Context context;
//获取运行时内存大小的五分之一作为内存缓存
final int MemorySize = (int) (Runtime.getRuntime().maxMemory() / 5);
//构造方法初始化context和缓存对象
public ImlCache(Context context) {
this.context = context;
initCache(context);
}
private void initCache(Context context) {
mLruCache = new LruCache<String, Bitmap>(MemorySize);
File file = getDiskCacheDir(context, "bitmapCache");
try {
mDiskLruCache = DiskLruCache.open(file, getAppVersion(), 1,
100 * 1024 * 1024);
} catch (IOException e) {
e.printStackTrace();
}
}
// 创建缓存目录
public File getDiskCacheDir(Context context, String uniqueName) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return new File(cachePath + File.separator + uniqueName);
}
// 获取当前版本号
public int getAppVersion() {
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
return info.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return 1;
}
//md5加密url路径
public String hashKeyForDisk(String key) {
String cacheKey;
try {
final MessageDigest mDigest = MessageDigest.getInstance("MD5");
mDigest.update(key.getBytes());
cacheKey = bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {
cacheKey = String.valueOf(key.hashCode());
}
return cacheKey;
}
private String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
//bitmap转换inputstream
public InputStream Bitmap2InputStream(Bitmap bm, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
return is;
}
@Override
public void put(String url, Bitmap bit) {
mLruCache.put(url, bit);
String key = hashKeyForDisk(url);
BufferedOutputStream out = null;
BufferedInputStream in = null;
try {
DiskLruCache.Editor editor = mDiskLruCache.edit(key);
if (editor != null) {
OutputStream outputStream = editor.newOutputStream(0);
in = new BufferedInputStream(Bitmap2InputStream(bit, 100),
8 * 1024);
out = new BufferedOutputStream(outputStream, 8 * 1024);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
editor.commit();
mDiskLruCache.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public Bitmap get(String url) {
Bitmap bitmap = null;
if (mLruCache.get(url) != null) {
bitmap = mLruCache.get(url);
return bitmap;
} else {
String key = hashKeyForDisk(url);
try {
DiskLruCache.Snapshot snapShot =mDiskLruCache.get(key);
if (snapShot != null) {
InputStream is = snapShot.getInputStream(0);
bitmap = BitmapFactory.decodeStream(is);
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
}
}
缓存类使用了lrucache作为内存缓存,使用disklrucache(一个磁盘缓存类)做磁盘缓存,缓存实现好了就开始写图片加载的类imageloader
public class ImageLoader {
private ImageCache mCache;
private downloadImage mDownload;
// 初始化线程池
ExecutorService mExecutorService = Executors.newFixedThreadPool(Runtime
.getRuntime().availableProcessors());
//这里初始化的是一个接口类型的缓存对象,后期可以通过依赖注入实现不同的缓存类
public ImageLoader(Context context) {
mCache = new ImlCache(context);
}
//图片加载方法传入url和imageview
public void displayImage(final String url, final ImageView iv) {
iv.setTag(url);
mExecutorService.submit(new Runnable() {
@Override
public void run() {
Bitmap bitmap = mCache.get(url);
if (bitmap == null) {
downloadImage down = new downloadImage(iv, url);
down.execute(url);
bitmap = down.doInBackground(url);
mCache.put(url, bitmap);
} else if (bitmap != null && iv.getTag().toString().equals(url)) {
iv.setImageBitmap(bitmap);
}
}
});
}
// load cancle
public void loadCancle() {
mDownload.onCancelled();
}
// 图片下载
class downloadImage extends AsyncTask<String, Integer, Bitmap> {
private ImageView iv;
private HttpURLConnection conn = null;
String url;
public downloadImage(ImageView iv, String url) {
this.iv = iv;
this.url = url;
}
@Override
protected Bitmap doInBackground(String... arg0) {
Bitmap bit = null;
try {
URL url = new URL(arg0[0]);
conn = (HttpURLConnection) url.openConnection();
bit = BitmapFactory.decodeStream(conn.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
return bit;
}
@Override
protected void onCancelled() {
super.onCancelled();
conn.disconnect();
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (iv.getTag() != null && url.equals(iv.getTag().toString())) {
iv.setImageBitmap(result);
}
}
}
}