Bitmap优化可做
1:像素优化,过大的原图缩小适当尺寸使用 2:内存复用 3:缓存
缓存一般有三级缓存:先内存中找,再磁盘中找,最后网络找。
Bitmap内存优化,如下可以调整bitmap大小和设计图一致,不失真。并且启用Bitmap复用
1)采样压缩:
public class BitmapUtil {
public static Bitmap resizeBitmap(Context context, int id, int maxW, int maxH, boolean hasAlpha, Bitmap reusable) {
Resources resources = context.getResources();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, id, options);
int w = options.outWidth;
int h = options.outHeight;
//1:采样率
//2:取值:2的整数幂(小于1则取值为1,任何其他值将向下舍入到最接近的2的幂)
options.inSampleSize = calcuteImSampleSize(w, h, maxW, maxH);
if(!hasAlpha) {
//如果没有透明度就用RGB_565节约内存,默认是ARGB_8888
options.inPreferredConfig = Bitmap.Config.RGB_565;
}
//关闭设置
options.inJustDecodeBounds = false;
//下面两句可以将Bitmap设置成能复用
options.inMutable = true;
options.inBitmap = reusable;
return BitmapFactory.decodeResource(resources, id, options);
}
private static int calcuteImSampleSize(int w, int h, int maxW, int maxH) {
int inSampleSize = 1;
if(w>maxH && h>maxH){
inSampleSize = 2;
while (w/inSampleSize>maxW && h/inSampleSize>maxH) {
inSampleSize*=2;
}
}
return inSampleSize;
}
}
2)质量压缩(不会压缩Bitmap的内存占用,保存成文件的时候大小变小了)
public static Bitmap compressImage(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
//循环判断如果压缩后图片是否大于50kb,大于继续压缩
while ( baos.toByteArray().length / 1024>50) {
//清空baos
baos.reset();
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
options -= 10;//每次都减少10
}
//把压缩后的数据baos存放到ByteArrayInputStream中
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
//把ByteArrayInputStream数据生成图片
Bitmap newBitmap = BitmapFactory.decodeStream(isBm, null, null);
return newBitmap;
}
3)缩放法压缩
通过Bitmap的静态方法createScaledBitmap来压缩Bitmap,必须对老的Bitmap做recycle并且置空回收。
public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight,
boolean filter) {
Matrix m = new Matrix();
final int width = src.getWidth();
final int height = src.getHeight();
if (width != dstWidth || height != dstHeight) {
final float sx = dstWidth / (float) width;
final float sy = dstHeight / (float) height;
m.setScale(sx, sy);
}
return Bitmap.createBitmap(src, 0, 0, width, height, m, filter);
}
LRU算法,最近最少使用的先丢弃。整体数据通过双向列表保存。
手写缓存框架
现在下载该地址项目下的三个文件拉到项目中
写个缓存类
public class ImageCache {
private static volatile ImageCache instance;
private LruCache<String, Bitmap> memoryCache;
private DiskLruCache diskLruCache;
BitmapFactory.Options options = new BitmapFactory.Options();
public static ImageCache getInstance(){
if(instance == null) {
synchronized (ImageCache.class) {
if(instance == null) {
instance = new ImageCache();
}
}
}
return instance;
}
//复用池
public static Set<WeakReference<Bitmap>> reuseablePool;
Context context;
//dir就是磁盘缓存存放数据的路径
public void init(Context context, String dir) {
this.context = context.getApplicationContext();
//复用池
reuseablePool = Collections.synchronizedSet(new HashSet<WeakReference<Bitmap>>());
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int memoryClass = am.getMemoryClass();
memoryCache = new LruCache<String, Bitmap>(memoryClass/8*1024*1024){
//return value 占用内存大小
@Override
protected int sizeOf(String key, Bitmap value) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){
return value.getByteCount();
}
return value.getAllocationByteCount();
}
@Override
protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
//oldValue就是从LRU中拿出来的Bitmap
if(oldValue.isMutable()) {//如果能复用
reuseablePool.add(new WeakReference<Bitmap>(oldValue, getReferenceQueue()));
} else {
oldValue.recycle();
}
}
};
try {
diskLruCache = DiskLruCache.open(new File(dir), 1, 1, 10*1024*1024);
} catch (Exception e) {
e.printStackTrace();
}
}
//引用队列
ReferenceQueue referenceQueue;
Thread clearReferenceQueue;
boolean shutDown;
//用于主动监听GC的API,加快回收
private ReferenceQueue<Bitmap> getReferenceQueue() {
if(referenceQueue == null) {
referenceQueue = new ReferenceQueue<Bitmap>();
clearReferenceQueue = new Thread(new Runnable() {
@Override
public void run() {
while (!shutDown) {
try {
//remove带阻塞功能,引用队列里面如果没有,就不会执行下去,会等待
Reference<Bitmap> reference = referenceQueue.remove();
Bitmap bitmap = reference.get();
if(bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
clearReferenceQueue.start();
}
return referenceQueue;
}
public void putBitmapToMemory(String key, Bitmap bitmap) {
memoryCache.put(key, bitmap);
}
public Bitmap getBitmapFromMemory(String key) {
return memoryCache.get(key);
}
public void clearMemoryCache() {
memoryCache.evictAll();
}
//获取复用池中的内容
public Bitmap getReuseable(int w, int h, int inSampleSize) {
//3.0一下不理会
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
return null;
}
Bitmap reuseable = null;
Iterator<WeakReference<Bitmap>> iterator = reuseablePool.iterator();
while (iterator.hasNext()) {
Bitmap bitmap = iterator.next().get();
if(null != bitmap){
//可以复用
if(checkInBitmap(bitmap, w, h, inSampleSize)) {
reuseable = bitmap;
iterator.remove();
Log.v("my_log", "复用池中找到了");
break;
}
}
}
return reuseable;
}
private boolean checkInBitmap(Bitmap bitmap, int w, int h, int inSampleSize) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return bitmap.getWidth() == w && bitmap.getHeight() == h && inSampleSize == 1;
}
if(inSampleSize >= 1) {
w/=inSampleSize;
h/=inSampleSize;
}
int byteCount = w*h*getPixelsCount(bitmap.getConfig());
return byteCount <= bitmap.getAllocationByteCount();
}
private int getPixelsCount(Bitmap.Config config) {
if(config == Bitmap.Config.ARGB_8888) {
return 4;
}
return 2;
}
//加入磁盘缓存
public void putBitmapToDisk(String key, Bitmap bitmap) {
DiskLruCache.Snapshot snapshot = null;
OutputStream os = null;
try {
snapshot = diskLruCache.get(key);
//如果缓存中已经有这个文件 不理他
if(null == snapshot) {
//如果没有这个文件,就生成这个文件
DiskLruCache.Editor editor = diskLruCache.edit(key);
if(null != editor) {
os = editor.newOutputStream(0);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, os);
editor.commit();
}
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(null != snapshot) {
snapshot.close();
}
if(null != os) {
try {
os.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
//从磁盘缓存中取
public Bitmap getBitmapFromDisk(String key, Bitmap reuseable) {
DiskLruCache.Snapshot snapshot = null;
Bitmap bitmap = null;
try {
snapshot = diskLruCache.get(key);
if(null == snapshot) {
return null;
}
//获取文件输入流,读取bitmap
InputStream is = snapshot.getInputStream(0);
//解码个图片,写入
options.inMutable = true;
options.inBitmap = reuseable;
bitmap = BitmapFactory.decodeStream(is, null, options);
if(null != bitmap) {
memoryCache.put(key, bitmap);
}
}catch (IOException e) {
e.printStackTrace();
}finally {
if(null != snapshot) {
snapshot.close();
}
}
return bitmap;
}
}
使用缓存来加载图片
//先注册
ImageCache.getInstance().init(this, "data/data/com.my.test");
//读取数据
Bitmap bitmap = ImageCache.getInstance().getBitmapFromMemory(key);
if(null == bitmap) {
Bitmap reuseable = ImageCache.getInstance().getReuseable(80, 80, 8);
bitmap = ImageCache.getInstance().getBitmapFromDisk(key, reuseable);
if(null == bitmap) {
bitmap = ImageResize.resizeBitmap(context, R.mipmap.xxx, 80, 80, false);
ImageCache.getInstance().putBitmapToDisk(key, bitmap);
Log.v("my_log", "从网络加载了数据");
}else {
Log.v("my_log", "从磁盘加载了数据");
}
}else {
Log.v("my_log", "从内存加载了数据");
}
四大图片框架对比
Glide(google),picasso(Square),Fresco(Facebook),imageLoader。
Glide的优势是三级缓存,支持GIF,webp甚至video,加载显示速度快,包体积小。默认bitmap格式RGB-565,更省内存,适合对图片没有高清要求。然后Glide缓存图片是按照ImageView的大小来缓存的,会为每种大小的ImageView缓存一次,这里会需要更大的缓存空间。
Picasso是二级缓存的,没有本地缓存。和Glide相比,他缓存了一个全尺寸的图片,根据需求的大小再压缩转换。
Fresco:综合了之前图片加载库的有点,在5.0以下的内存优化非常好,但它的不足是体积太大,所以如果应用没有太多图片需求,不推荐使用Fresco。
Glide和Picasso相比:Picasso.with()只能传入上下文。Glide.with()可以传上下文,Activity,FragmentActivity,Fragment的实例等。Picasso采用ARGB-8888,Glide采用RGB-565。Picasso加载的是全图,图片质量和清晰度比Glide高,更容易OOM。Glide可以加载GIF和webp,而Picasso能加载webp不能加载GIF。Glide缓存的图片和ImageView尺寸相同,而Picasso缓存的是全尺寸。针对于不同大小的ImageView,Glide会为他们分别缓存对应大小的图片,而Picasso只缓存全尺寸的图,使用时压缩到对应尺寸,所以Glide加载更快。
总而言之:Glide比Picasso加载速度要快,但是Glide比Picasso需要更多的空间来缓存;Glide加载图像以及磁盘缓存的方式,都优于Picasso,且Glide更有利于减少OutOfMemoryError的发生。而且Glide能加载GIF。Picasso的优势在于包体积小,加载的图片质量更高。
Glide相比ImageLoader:Glide支持图片加载优先级,使用okhttp加载,支持GIF和webp,支持缩略图,支持生命周期,并且ImageLoader不再维护。
一个比较好的图片加载框架需要实现的需求包括下面这些:
- 可以灵活配置
- 支持高并发,优先级设置
- 支持不同的加载策略
- 三级缓存
- 支持生命周期管理
- 对缓存策略可以扩展
- 支持占位图片加载
- 动图加载
- 图片显示自适应
- 支持请求转发,下载