Bitmap 详细介绍

351 阅读5分钟

Bitmap 是 Android 应用程序中用来表示图像的类,提供了多种方法来操作和处理图像数据。下面是 Bitmap 类的全部方法介绍:

构造方法

创建一个指定宽度、高度和像素格式的空白 Bitmap 对象 :

Bitmap(int width, int height, Bitmap.Config config)

创建一个指定宽度、高度、像素格式和是否可变的空白 Bitmap 对象:

  Bitmap(int width, int height, Bitmap.Config config, boolean isMutable)

将指定 Bitmap 对象的像素数据拷贝到新的 Bitmap 对象中:

 Bitmap(android.graphics.Bitmap source)

将指定 Bitmap 对象的像素数据拷贝到新的 Bitmap 对象中,同时指定是否可变:

   Bitmap(android.graphics.Bitmap source, boolean isMutable)

创建一个指定宽度、高度和像素格式的空白 Bitmap 对象:

Bitmap.createBitmap(int width, int height, Bitmap.Config config)

从给定的颜色数组中创建一个包含指定宽度、高度和像素格式的 Bitmap 对象:

 Bitmap.createBitmap(int\[\] colors, int offset, int stride, int width, int height, Bitmap.Config config)

从指定 Bitmap 对象中截取一部分像素数据创建一个新的 Bitmap 对象:

 Bitmap.createBitmap(android.graphics.Bitmap source, int x, int y, int width, int height)

将指定 Bitmap 对象中的一部分像素数据应用指定的变换矩阵后创建一个新的 Bitmap 对象:

Bitmap.createBitmap(android.graphics.Bitmap source, int x, int y, int width, int height, android.graphics.Matrix matrix, boolean filter)

将指定 Bitmap 对象缩放并创建一个新的 Bitmap 对象:

 Bitmap.createBitmap(android.graphics.Bitmap source, int width, int height, android.graphics.Bitmap.Config config)

缩放指定 Bitmap 对象并返回一个新的 Bitmap 对象:

 Bitmap.createScaledBitmap(android.graphics.Bitmap src, int dstWidth, int dstHeight, boolean filter)

基本方法

创建当前 Bitmap 对象的一份副本,副本的像素格式和可变性可以自定义:

copy(Bitmap.Config config, boolean isMutable)

从指定的 Buffer 中拷贝像素数据到当前 Bitmap 对象中:

copyPixelsFromBuffer(java.nio.Buffer src)

将当前 Bitmap对象中的像素数据拷贝到指定的 Buffer 中:

copyPixelsToBuffer(java.nio.Buffer dst)

不支持 Parcel 序列化,返回 0:

describeContents()

使用指定的颜色填充当前 Bitmap 对象的所有像素:

eraseColor(int color)

从当前 Bitmap 对象中提取包含 alpha 通道信息的新的 Bitmap 对象:

extractAlpha()

从当前 Bitmap 对象中提取包含 alpha 通道信息的新的 Bitmap 对象,同时按照给定的 Paint 对象进行颜色过滤:

extractAlpha(Paint paint, int\[\] offset)

获取当前 Bitmap 对象所占用字节数(包括像素数据和内存管理开销等):

getAllocationByteCount()

获取当前 Bitmap 对象的像素数据占用字节数:

getByteCount()

获取当前 Bitmap 对象的像素格式:

getConfig()

获取当前 Bitmap 对象的像素密度(DPI):

getDensity()

获取当前 Bitmap 对象的高度:

getHeight()

获取当前 Bitmap 对象的宽度:

getWidth()

如果当前 Bitmap 对象是一个 NinePatch 阴影,则返回其对应的 chunk 数据,否则返回 null:

getNinePatchChunk()

获取当前 Bitmap 对象中指定位置的像素颜色值:

getPixel(int x, int y)

将当前 Bitmap 对象中指定区域的像素颜色值复制到给定的颜色数组中:

getPixels(int\[\] pixels, int offset, int stride, int x, int y, int width, int height)

判断当前 Bitmap 对象是否包含 alpha 通道信息:

hasAlpha()

判断当前 Bitmap 对象是否可变:

isMutable()

判断当前 Bitmap 对象是否已经被回收:

isRecycled()

提示系统预加载当前 Bitmap 对象的像素数据,以提高绘制效率和性能:

prepareToDraw()

回收当前 Bitmap 对象的内存空间,释放资源:

recycle()

设置当前 Bitmap 对象的像素密度(DPI):

setDensity(int density)

设置当前 Bitmap 对象是否包含 alpha 通道信息:

setHasAlpha(boolean hasAlpha)

设置当前 Bitmap 对象中指定位置的像素颜色值:

setPixel(int x, int y, int color)

将给定的颜色数组中的像素颜色值复制到当前 Bitmap 对象中指定区域的像素数据中:

setPixels(int\[\] pixels, int offset, int stride, int x, int y, int width, int height)

压缩方法

将当前 Bitmap 对象压缩成指定格式和质量的图像数据,并写入到给定的输出流中:

compress(Bitmap.CompressFormat format, int quality, java.io.OutputStream stream)

将当前 Bitmap 对象压缩成指定格式和质量的图像数据,并写入到给定的通道中:

compress(Bitmap.CompressFormat format, int quality, java.nio.channels.WritableByteChannel channel)

将当前 Bitmap 对象压缩成指定格式和质量的图像数据,并写入到指定的文件中:

compress(Bitmap.CompressFormat format, int quality, java.io.File file)

将当前 Bitmap 对象压缩成指定格式和质量的图像数据,并写入到指定文件名的文件中:

compress(Bitmap.CompressFormat format, int quality, java.lang.String fileName)

高级方法

生成当前 Bitmap 对象的一组 Mipmap,用于实现纹理映射等高级图形渲染功能:

generateMipMap()

同上,但可以指定是否允许重用当前 Bitmap 对象来存储 Mipmap 数据:

generateMipMap(boolean canReuse)

获取当前 Bitmap 对象内存的 Allocation 对象(需要支持 RenderScript):

getAllocation()

获取当前 Bitmap 对象的颜色空间信息(需要 API 26+ 支持):

getColorSpace()

获取当前 Bitmap 对象的版本号,用于判断对象是否被修改过:

getGenerationId()

获取当前 Bitmap 对象的每行像素数据所占用的字节数:

getRowBytes()

重新配置当前 Bitmap 对象的宽度、高度和像素格式等属性:

reconfigure(int width, int height, Bitmap.Config config)

同 setPixel(int x, int y, int color),但可以指定 alpha 通道类型:

setPixel(int x, int y, int color, int alphaType)

设置当前 Bitmap 对象是否预乘 alpha 值:

setPremultiplied(boolean premultiplied)

获取指定资源 ID 对应的 Bitmap 图像的宽度:

getWidth(int id)

获取指定资源 ID 对应的 Bitmap 图像的高度:

getHeight(int id)

以上就是 Bitmap 类的全部方法介绍,在实际开发中根据需求选择合适的方法来操作 Bitmap 对象,可以实现很多有趣和有用的图像处理功能。