Android Bitmap计算大小 getRowBytes和g

作者: 我是吸血鬼 | 来源:发表于2018-03-01 21:44 被阅读108次

int size = bitmap.getRowBytes() * bitmap.getHeight();

获取大小,Bitmap所占用的内存空间数等于Bitmap的每一行所占用的空间数乘以Bitmap的行数

可以获取Bitmap大小的方法有如下几种:

  1. getRowBytes:Since API Level 1
  2. getByteCount:Since API Level 12
  3. getAllocationByteCount:Since API Level 19

兼容性解决方案:

 public static int getBitmapSize(Bitmap bitmap) {  
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  //SInce API 19  
         return bitmap.getAllocationByteCount();  
     }  
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { //Since API 12  
         return bitmap.getByteCount();  
     }  
  
     return bitmap.getRowBytes() * bitmap.getHeight();               
 } 

Note:

如果位图被重用、解码更小尺寸的其他位图或重新配置,getAllocationByteCount()的计算结果有可能比getByteCount()的结果大一些。

相关文章

网友评论

    本文标题:Android Bitmap计算大小 getRowBytes和g

    本文链接:https://www.haomeiwen.com/subject/xbwnxftx.html