Android 从源码分析Bitmap和BitmapFactory常用API,kotlin开发android教程

本文深入探讨了Android中Bitmap和BitmapFactory的常见API,包括isRecycled(), getWidth(), getHeight(), 等方法,以及压缩、配置、缩放等操作。通过源码分析,理解位图内存管理与性能优化的关键点。" 104735385,8524893,Java实现逆波兰表达式计算器,"['算法', '数据结构', 'java', '栈']
摘要由CSDN通过智能技术生成

}

mRecycled = true;

}

}

isRecycled()

判断位图内存是否已经被回收。

checkRecycled

如果已经被回收,则抛出非法状态异常IllegalStateException。

/**

  • This is called by methods that want to throw an exception if the bitmap

  • has already been recycled.

*/

private void checkRecycled(String errorMessage) {

if (mRecycled) {

throw new IllegalStateException(errorMessage);

}

}

getWidth() 获取宽度
getHeight() 获取高度
getScaledWidth(Canvas canvas)

获取经指定密度转换后的宽度

/**

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

  • density of the given {@link Canvas}.

*/

public int getScaledWidth(Canvas canvas) {

return scaleFromDensity(getWidth(), mDensity, canvas.mDensity);

}

/**

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

  • density of the given {@link DisplayMetrics}.

*/

public int getScaledWidth(DisplayMetrics metrics) {

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

}

/**

  • @hide

*/

static public int scaleFromDensity(int size, int sdensity, int tdensity) {

if (sdensity == DENSITY_NONE || tdensity == DENSITY_NONE || sdensity == tdensity) {

return size;

}

// Scale by tdensity / sdensity, rounding up.

return ((size * tdensity) + (sdensity >> 1)) / sdensity;

}

getScaledHeight(Canvas canvas)

获取经制定密度转换后的高度

/**

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

  • density of the given {@link Canvas}.

*/

public int getScaledHeight(Canvas canvas) {

return scaleFromDensity(getHeight(), mDensity, canvas.mDensity);

}

/**

  • 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 BitmapF

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值