一、本地缓存类 ImageUtils
private static String path = Environment.getExternalStorageDirectory() + “/giftwash/images”;
//判断本地内存
public static boolean isMounted() {
return Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageDirectory());
}
/**
* 保存图片方法
*
* @param url
* @param bitmap
*/
public static void saveImage(String url, Bitmap bitmap) {
if (!isMounted()) {
return;
}
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100,
new FileOutputStream(new File(file, “” + url.hashCode())));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 去图片方法
*
* @param url
* @return
*/
public static Bitmap getImage(String url) {
if (!isMounted())
return null;
File file = new File(path, "" + url.hashCode());
if (file.exists()) {
return BitmapFactory.decodeFile(file.getAbsolutePath());
}
return null;
}
二、重写ImageView
/**
* 下载及图片缓存
*/
public class CusImgeView extends ImageView implements MyDownUtils.OnDownCompelet {
private int defaultImg;//默认显示图片
public CusImgeView(Context context) {
super(context);
parseAttr(null);
}
public CusImgeView(Context context, AttributeSet attrs) {
super(context, attrs);
parseAttr(attrs);
}
/**
* 自定义解析属性
* @param attrs
*/
private void parseAttr(AttributeSet attrs) {
TypedArray ta = getResources().obtainAttributes(attrs, R.styleable.cusImgeView);
defaultImg = ta.getResourceId(R.styleable.cusImgeView_defaultImg,0);
ta.recycle();
this.setImageResource(defaultImg);
}
/**
* 一级缓存
*/
private static LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(
1024 * 1024 * 10) {
@Override
protected void entryRemoved(boolean evicted, String key,
Bitmap oldValue, Bitmap newValue) {
super.entryRemoved(evicted, key, oldValue, newValue);
if (evicted) {// 二级缓存
mapCache.put(key, new SoftReference<Bitmap>(oldValue));
}
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
/**
* 二级缓存
*/
private static Map<String, SoftReference<Bitmap>> mapCache = new HashMap<String, SoftReference<Bitmap>>();
/**
* 放入缓存
*
* @param url
*/
public static void addCache(String url, Bitmap bitmap) {
if (bitmap != null) {
lruCache.put(url, bitmap);// 存入缓存
ImageUtils.saveImage(url,bitmap);
// mapCache.put(url, new SoftReference<Bitmap>(bitmap));
}
}
public static Bitmap getCache(String url){
Bitmap bitmap = null;
bitmap = lruCache.get(url);
if(bitmap == null){
if(mapCache.containsKey(url)){
SoftReference<Bitmap> soft = mapCache.get(url);
bitmap = soft.get();
if(bitmap != null){
lruCache.put(url,bitmap);//放到一级缓存中去
mapCache.remove(url);//移除该图片
}
}
}
if(bitmap == null){
//到本地区拿
bitmap = ImageUtils.getImage(url);
if(bitmap!=null)
lruCache.put(url,bitmap);
}
return bitmap;
}
/**
* 获取图片调用方法
* @param url
*/
public void setUrl(String url){
if(defaultImg!=0) {
this.setImageResource(defaultImg);
}
setTag(url);
Bitmap bitmap = getCache(url);
if(bitmap == null)
//new SortImgAsync(AppUtils.getContext(),this,url);
MyDownUtils.downBitmap(url,this);
else
setImageBitmap(bitmap);
}
@Override
public void downSucc(String url, Object obj) {
Bitmap bitmap = null;
if(obj!=null && url.equals(getTag())) {
bitmap = (Bitmap) obj;
setImageBitmap(bitmap);
addCache(url, bitmap);
}
}
}
三、在布局添加载重写之后的ImageView,调用setUrl便可加载图片