您的位置:首页 > 移动开发 > Android开发

Android开发艺术探索读书笔记(第12章 Bitmap的加载和Cache)

2018-02-26 13:26 302 查看

Bitmap

Bitmap占用的内存大小可以通过下面的API获取 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
int byteCount = bitmap.getByteCount();
int allocationByteCount = bitmap.getAllocationByteCount();API19之前 /**
* Returns the minimum number of bytes that can be used to store this bitmap's pixels.
*
* <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, the result of this method can
* no longer be used to determine memory usage of a bitmap. See {@link
* #getAllocationByteCount()}.</p>
*/
public final int getByteCount() {
if (mRecycled) {
Log.w(TAG, "Called getByteCount() on a recycle()'d bitmap! "
+ "This is undefined behavior!");
return 0;
}
// int result permits bitmaps up to 46,340 x 46,340
return getRowBytes() * getHeight();
}API19之后 /**
* Returns the size of the allocated memory used to store this bitmap's pixels.
*
* <p>This can be larger than the result of {@link #getByteCount()} if a bitmap is reused to
* decode other bitmaps of smaller size, or by manual reconfiguration. See {@link
* #reconfigure(int, int, Config)}, {@link #setWidth(int)}, {@link #setHeight(int)}, {@link
* #setConfig(Bitmap.Config)}, and {@link BitmapFactory.Options#inBitmap
* BitmapFactory.Options.inBitmap}. If a bitmap is not modified in this way, this value will be
* the same as that returned by {@link #getByteCount()}.</p>
*
* <p>This value will not change over the lifetime of a Bitmap.</p>
*
* @see #reconfigure(int, int, Config)
*/
public final int getAllocationByteCount() {
if (mRecycled) {
Log.w(TAG, "Called getAllocationByteCount() on a recycle()'d bitmap! "
+ "This is undefined behavior!");
return 0;
}
return nativeGetAllocationByteCount(mNativePtr);
}

bitmap占用的内存大小等于bitmapWidth*bitmapHeight*bitmap像素格式,常用的像素格式有下面两种
ARGB_8888:4byte
RGB_565:2byte
在实际开发中,bitmap占用的内存大小还和图片存放的目录和设备的密度有关,举个例子
一张522*686的PNG 图片,我把它放到 drawable-xxhdpi 目录下,在三星s6上加载,占用内存2547360B,
其中 density 对应 xxhdpi 为480,targetDensity 对应三星s6的密度为640
: 522/480 * 640 * 686/480 *640 * 4 = 2546432B

如何减少Bitmap占用的内存
1.采样inSampleSize
2.使用矩阵 
采样之后bitmap占用的内存变小了,但是图片也变小了,想用canvas绘制这张图,怎么办?可以使用矩阵
代码如下@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mMatrix.preScale(2,2,0,0);
canvas.drawBitmap(mBitmap,mMatrix,mPaint);
}这样,绘制出来的图就是放大以后的效果了,不过占用的内存却仍然是我们采样出来的大小。 如果我要把图片放到 ImageView 当中呢?一样可以,请看:imageView.setImageMatrix(mMatrix);
imageView.setScaleType(ImageView.ScaleType.MATRIX);
imageView.setImageBitmap(mBitmap);

Android中的缓存策略

缓存要实现的效果如下:
当程序第一次从网络上加载图片成功以后,就将其缓存到内存和存储设备上,这样当程序打算从网络上加载图片时,先从内存中
获取,如果没有,从存储设备上获取,如果还没有,则从网络加载。因为从内存中加载比设备加载更快,这样既提高了效率又节省了流量。
目前常用的缓存策略是LRU,即最近最少使用算法,当缓存容量达到指定的上限时,移除最近最少使用的缓存。接下来我们介绍LruCache和DiskLruCache.
LruCache使用
//缓存的总容量大小
int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 8);
LruCache<String, Bitmap> memoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
//每个缓存内容的大小,单位为KB
protected int sizeOf(String key, Bitmap value) {
//getByteCount返回的是字节,1KB=1024B
return value.getByteCount() / 1024;
}
//移除旧缓存会触发此方法,可以完成资源回收工作
@Override
protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {

}
};
通过LruCache的put和get方法就可以进行缓存的添加和获取,remove可以删除缓存。

DiskLruCache使用

DiskLruCache用来实现磁盘缓存,项目地址如下
https://github.com/JakeWharton/DiskLruCache
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: