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

BitMap的getRowBytes和getByteCount()

2017-02-06 00:41 92 查看
getRowBytes():每一行所占的空间数。

getByteCount():BitMap的大小。

为什么在一般情况下不用bitmap.getByteCount()呢?

因为getByteCount要求的API版本较高,考虑到兼容性,一般使用上面的getRowBytes方法。

getRowBytes:Since API Level 1

getByteCount:Since API Level 12

源码:

public final int getByteCount() {
return getRowBytes() * getHeight();
}


所以,getByteCount()方法也就是实现了一下简单的封装。在开发中如果版本有要求可以使用下面代码,或者直接使用getRowBytes() * getHeight();

/**
* 得到bitmap的大小
*/
public static int getBitmapSize(Bitmap bitmap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    //API 19
return bitmap.getAllocationByteCount();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
return bitmap.getByteCount();
}
// 在低版本中用一行的字节x高度
return bitmap.getRowBytes() * bitmap.getHeight();                //earlier version
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  api BitMap Android