您的位置:首页 > 编程语言

Bitmap.getByteCount()实现源代码

2013-11-19 11:55 141 查看
关于Bitmap的内存使用和优化想必很多人头疼过,好在看过前辈的一篇文章,文章的链接显示在这里 Android照片墙应用实现,再多的图片也不怕崩溃,不过其中有一段代码只有在android API12及以上的版本才能使用,这句代码就是Bitmap.getByteCount(),该方法的注释说明是:获取Bitmap能够存储的最小字节数。那么如何在API12以下的Bitmap获取其占位大小呢?

下面这个方法可以替代Bitmap.getByteCount():

/**
* 相当于Bitmap.getByteCount()方法,获取Bitmap存储用的最小字节数量
* @param bitmap 用于计算的Bitmap
* @return Bitmap存储用的最小字节数量
*/
public static int getByteCount(Bitmap bitmap) {
Config config = bitmap.getConfig();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int pixelByte = 4;//大多数图片默认Config.ARGB_8888
if (config == Config.ARGB_8888) {
pixelByte = 4;
} else if (config == Config.ALPHA_8) {
pixelByte = 1;
} else if (config == Config.ARGB_4444) {
pixelByte = 2;
} else if (config == Config.RGB_565) {
pixelByte = 2;
}
return width*height*pixelByte;
}

分享常在小v博客。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android bitmap getCo
相关文章推荐