/**
* 图片加载工具类(三级缓存)
*
* 注意:
* 1.首次加载 Android App 时,肯定要通过网络交互来获取图片,之后我们可以将图片保存至本地SD卡和内存中
* 2.之后运行 App 时,优先访问内存中的图片缓存,若内存中没有,则加载本地SD卡中的图片
* 3.总之,只在初次访问新内容时,才通过网络获取图片资源
* Created by zxp on 2016/6/17.
*/
public class ImageLoaderUtils {
private static volatile ImageLoaderUtils imageLoaderUtils;
private MenoryCacheUtils menoryCacheUtils;//内存缓存
private LocalCacheUtils localCacheUtils;//本地缓存
private NetBitmapUtils netBitmapUtils;//网络加载
//单例模式
public static ImageLoaderUtils getInstance() {
if (null == imageLoaderUtils) {
synchronized (ImageLoaderUtils.class) {
if (null == imageLoaderUtils) {
imageLoaderUtils = new ImageLoaderUtils();
}
}
}
return imageLoaderUtils;
}
/**
* 加载图片
*
* @param imageView 图片视图
* @param url 图片地址
*/
public void setImageViewFromBitmap(ImageView imageView, String url) {
setImageViewFromBitmap(imageView, url, NetBitmapUtils.BITMAP_INSAMPLESIZE_ONE);
}
/**
* 加载图片
*
* @param imageView 图片视图
* @param url 图片地址
* @param compressNumber 压缩倍率
*/
public void setImageViewFromBitmap(ImageView imageView, String url, int compressNumber) {
if (menoryCacheUtils == null) {
menoryCacheUtils = new MenoryCacheUtils();
}
if (localCacheUtils == null) {
localCacheUtils = new LocalCacheUtils();
}
if (netBitmapUtils == null) {
netBitmapUtils = new NetBitmapUtils(menoryCacheUtils, localCacheUtils);
}
Bitmap bitmap;
//从内存缓存
bitmap = menoryCacheUtils.getBitmap(url);
if (bitmap != null) {
setImageView(imageView, bitmap);
return;
}
//本地缓存
bitmap = localCacheUtils.getBitmap(url);
if (bitmap != null) {
setImageView(imageView, bitmap);
menoryCacheUtils.saveBitmap(url, bitmap);
return;
}
//网络获取
netBitmapUtils.getBitmap(imageView, url, compressNumber);
}
//设置图片
private void setImageView(ImageView imageView, Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
}
/**
* 内存缓存
* Created by zxp on 2016/6/17.
*/
public class MenoryCacheUtils {
private LruCache lruCache;
public MenoryCacheUtils() {
//通常得到手机最大内存的1/8,如果app在使用过程中超过这个,会自动回收
long maxMenory = Runtime.getRuntime().maxMemory() / 8;
lruCache = new LruCache((int) maxMenory) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
}
/**
* 写入图片到内存
*
* @param url 地址
* @param bitmap 图片
*/
public void saveBitmap(String url, Bitmap bitmap) {
lruCache.put(url, bitmap);
}
/**
* 从内存中拿到图片
*
* @param url 地址
*/
public Bitmap getBitmap(String url) {
return lruCache.get(url);
}
}
/**
* 本地缓存工具类
* Created by zxp on 2016/6/17.
*/
public class LocalCacheUtils {
//文件村粗的路径
private static final String CACHE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/ImageCache";
/**
* 拿到本地存储的图片
*
* @param url 地址
* @return
*/
public Bitmap getBitmap(String url) {
try {
//截取文件名字
String fileName = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
File file = new File(CACHE_PATH, fileName);
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 保存图片到本地
*
* @param url 地址
* @param bitmap 图片
*/
public void saveBitmap(String url, Bitmap bitmap) {
try {
String fileName = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
File file = new File(CACHE_PATH, fileName);
//判断父目录是否存在,不存在新建一个
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
//保存图片到本地,
// 注意第二个参数:如果不压缩是100,表示压缩率为0,如果是30,表示压缩70%;
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
}
**
* 网络加载图片工具类
* 网络请求拿到的结果数据bitmap,再保存在本地和内存中
* Created by zxp on 2016/6/17.
*/
public class NetBitmapUtils {
public static final int BITMAP_INSAMPLESIZE_ONE = 1;//原图
public static final int BITMAP_INSAMPLESIZE_HALF = 2;//宽高变为1/2
public static final int BITMAP_INSAMPLESIZE_ONE_THIRD = 3;//宽高变为1/3
public static final int BITMAP_INSAMPLESIZE_ONE_QUARTER = 4;//宽高变为1/4
private LocalCacheUtils localCacheUtils;
private MenoryCacheUtils menoryCacheUtils;
//图片压缩倍率(比如:1为不压缩,2为压缩宽高为一半)
private int compressNumber = 1;
public NetBitmapUtils(MenoryCacheUtils menoryCacheUtils, LocalCacheUtils localCacheUtils) {
this.menoryCacheUtils = menoryCacheUtils;
this.localCacheUtils = localCacheUtils;
}
public NetBitmapUtils(MenoryCacheUtils menoryCacheUtils, LocalCacheUtils localCacheUtils, int compressNumber) {
this.menoryCacheUtils = menoryCacheUtils;
this.localCacheUtils = localCacheUtils;
this.compressNumber = compressNumber;
}
/**
* 网络下载图片
*
* @param iv 显示图片的imagview
* @param url 网络下载的地址
*/
public void getBitmap(ImageView iv, String url) {
getBitmap(iv, url, compressNumber);
}
/**
* 网络下载图片
*
* @param iv 显示图片的imagview
* @param url 网络下载的地址
* @param compressnumber 图片压缩倍率(1为不压缩)
*/
public void getBitmap(ImageView iv, String url, int compressnumber) {
this.compressNumber = compressnumber;
new ImageViewTask().execute(iv, url, compressnumber);
}
/**
* AsyncTask就是对handler和线程池的封装
* 第一个泛型:参数类型
* 第二个泛型:更新进度的泛型
* 第三个泛型:onPostExecute的返回结果
*/
class ImageViewTask extends AsyncTask
ImageLoaderUtils.getInstance().setImageViewFromBitmap(imageView , url);
图片宽高压缩为原来一半:
ImageLoaderUtils.getInstance().setImageViewFromBitmap(imageView , url , 2);
github的Demo