如何测量或估计不支持bitmap.getByteCount()的Android版本中的位图大小

Jan*_*usz 0 android bitmap android-image

我最近在Android上关注了Googles Advice on Bitmap Caching,并 从支持库中添加了我自己的LruCache.如果缓存已知图像的大小(以字节为单位),则LruCache的效果最佳.

问题是,位图的getByteCount仅在Android API Level 11之后可用.

您如何猜测内存中位图的大小?

sta*_*zel 5

从android.graphics.Bitmap源代码:

public final int getByteCount() {
        // int result permits bitmaps up to 46,340 x 46,340
        return getRowBytes() * getHeight();
    }
Run Code Online (Sandbox Code Playgroud)

这两个getRowBytes()getHeight()是因为API等级1,这样你就可以实现自己的自己getByteCount()的Android的所有版本.乍一看,似乎getByteCount()只是为了方便起见而添加的.