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

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值