BitMap的getRowBytes和getByteCount()

    xiaoxiao2021-03-26  30

    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 }
    转载请注明原文地址: https://ju.6miu.com/read-662750.html

    最新回复(0)