Android 計算Bitmap大小


今天使用LruCache寫demo的時候,要獲取Bitmap的大小

於是就用到了

 

return bitmap.getRowBytes() * bitmap.getHeight();// 獲取大小並返回
//Bitmap所占用的內存空間數等於Bitmap的每一行所占用的空間數乘以Bitmap的行數


為什么不用bitmap.getByteCount()呢?
因為getByteCount要求的API版本較高,考慮到兼容性使用上面的方法


1、getRowBytes:Since API Level 1
2、getByteCount:Since API Level 12


查看Bitmap源碼
  

[java]  view plain  copy
 
 print?
  1. public final int getByteCount() {  
  2.       return getRowBytes() * getHeight();  
  3.   }  


所以API 12 以后
getByteCount() = getRowBytes() * getHeight();


在計算Bitmap所占空間時上面的方法或許有幫助。

 

補充:

 

[java]  view plain  copy
 
 print?
    1. /** 
    2.   * 得到bitmap的大小 
    3.   */  
    4.  public static int getBitmapSize(Bitmap bitmap) {  
    5.      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    //API 19  
    6.          return bitmap.getAllocationByteCount();  
    7.      }  
    8.      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12  
    9.          return bitmap.getByteCount();  
    10.      }  
    11.      // 在低版本中用一行的字節x高度  
    12.      return bitmap.getRowBytes() * bitmap.getHeight();                //earlier version  
    13.  }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM