Android 从源码分析Bitmap和BitmapFactory常用API

本文详细介绍了Android中Bitmap的几个关键API,包括getRowBytes()、getByteCount()、getAllocationByteCount()等,以及BitmapFactory的使用,如设置是否可变、缩放比例、压缩格式等。通过对源码的分析,帮助读者理解Bitmap内存管理和操作的底层原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • Convenience for calling {@link #getScaledHeight(int)} with the target

  • density of the given {@link DisplayMetrics}.

*/

public int getScaledHeight(DisplayMetrics metrics) {

return scaleFromDensity(getHeight(), mDensity, metrics.densityDpi);

}

getRowBytes()

获取native位图行像素的byte数

/**

  • Return the number of bytes between rows in the bitmap’s pixels. Note that

  • this refers to the pixels as stored natively by the bitmap. If you call

  • getPixels() or setPixels(), then the pixels are uniformly treated as

  • 32bit values, packed according to the Color class.

  • As of {@link android.os.Build.VERSION_CODES#KITKAT}, this method

  • should not be used to calculate the memory usage of the bitmap. Instead,

  • see {@link #getAllocationByteCount()}.

  • @return number of bytes between rows of the native bitmap pixels.

*/

public final int getRowBytes() {

if (mRecycled) {

Log.w(TAG, “Called getRowBytes() on a recycle()'d bitmap! This is undefined behavior!”);

}

return nativeRowBytes(mNativePtr);

}

getByteCount()

获取可以存储此bitmap像素的最小字节。返回行像素x高度。

/**

  • Returns the minimum number of bytes that can be used to store this bitmap’s pixels.

  • As of {@link android.os.Build.VERSION_CODES#KITKAT}, the result of this method can

  • no longer be used to determine memory usage of a bitmap. See {@link

  • #getAllocationByteCount()}.

*/

public final int getByteCount() {

// int result permits bitmaps up to 46,340 x 46,340

return getRowBytes() * getHeight();

}

getAllocationByteCount()

获取用来存储bitmap所分配的内存大小。在bitmap的生命周期内此值不会改变。

/**

  • Returns the size of the allocated memory used to store this bitmap’s pixels.

  • This can be larger than the result of {@link #getByteCount()} if a bitmap is reused to

  • decode other bitmaps of smaller size, or by manual reconfiguration. See {@link

  • #reconfigure(int, int, Config)}, {@link #setWidth(int)}, {@link #setHeight(int)}, {@link

  • #setConfig(Bitmap.Config)}, and {@link BitmapFactory.Options#inBitmap

  • BitmapFactory.Options.inBitmap}. If a bitmap is not modified in this way, this value will be

  • the same as that returned by {@link #getByteCount()}.

  • This value will not change over the lifetime of a Bitmap.

  • @see #reconfigure(int, int, Config)

*/

public final int getAllocationByteCount() {

if (mBuffer == null) {

// native backed bitmaps don’t support reconfiguration,

// so alloc size is always content size

return getByteCount();

}

return mBuffer.length;

}

getConfig()

获取Config枚举:

  • ALPHA_8, 代表8位Alpha位图,每个像素占用1byte内存

  • RGB_565,代表8位RGB位图,每个像素占用2byte内存

  • ARGB_4444 (@deprecated),代表16位ARGB位图,每个像素占用2byte内存

  • ARGB_8888,代表32位ARGB位图,每个像素占用4byte内存

/**

  • If the bitmap’s internal config is in one of the public formats, return

  • that config, otherwise return null.

*/

public final Config getConfig() {

if (mRecycled) {

Log.w(TAG, “Called getConfig() on a recycle()'d bitmap! This is undefined behavior!”);

}

return Config.nativeToConfig(nativeConfig(mNativePtr));

}

hasAlpha()

检测bitmap是否支持像素级别透明。

/** Returns true if the bitmap’s config supports per-pixel alpha, and

  • if the pixels may contain non-opaque alpha values. For some configs,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值