Bitmap 介绍(详细版)

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

构造方法

Bitmap(int width, int height, Bitmap.Config config):创建一个指定宽度、高度和像素格式的空白 Bitmap 对象。

Bitmap(int width, int height, Bitmap.Config config, boolean isMutable):创建一个指定宽度、高度、像素格式和是否可变的空白 Bitmap 对象。

Bitmap(android.graphics.Bitmap source):将指定 Bitmap 对象的像素数据拷贝到新的 Bitmap 对象中。

Bitmap(android.graphics.Bitmap source, boolean isMutable):将指定 Bitmap 对象的像素数据拷贝到新的 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.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 对象并返回一个新的 Bitmap 对象。

基本方法

copy(Bitmap.Config config, boolean isMutable):创建当前 Bitmap 对象的一份副本,副本的像素格式和可变性可以自定义。

copyPixelsFromBuffer(java.nio.Buffer src):从指定的 Buffer 中拷贝像素数据到当前 Bitmap 对象中。

copyPixelsToBuffer(java.nio.Buffer dst):将当前 Bitmap 对象中的像素数据拷贝到指定的 Buffer 中。

describeContents():不支持 Parcel 序列化,返回 0。

eraseColor(int color):使用指定的颜色填充当前 Bitmap 对象的所有像素。

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

extractAlpha(Paint paint, int[] offset):从当前 Bitmap 对象中提取包含 alpha 通道信息的新的 Bitmap 对象,同时按照给定的 Paint 对象进行颜色过滤。

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

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

getConfig():获取当前 Bitmap 对象的像素格式。

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

getHeight():获取当前 Bitmap 对象的高度。

getWidth():获取当前 Bitmap 对象的宽度。

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

getPixel(int x, int y):获取当前 Bitmap 对象中指定位置的像素颜色值。

getPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height):将当前 Bitmap 对象中指定区域的像素颜色值复制到给定的颜色数组中。

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

isMutable():判断当前 Bitmap 对象是否可变。

isRecycled():判断当前 Bitmap 对象是否已经被回收。

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

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

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

setHasAlpha(boolean hasAlpha):设置当前 Bitmap 对象是否包含 alpha 通道信息。

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 对象压缩成指定格式和质量的图像数据,并写入到指定文件名的文件中。

高级方法

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

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

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

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

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

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

reconfigure(int width, int height, Bitmap.Config config):重新配置当前 Bitmap 对象的宽度、高度和像素格式等属性。

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

setPremultiplied(boolean premultiplied):设置当前 Bitmap 对象是否预乘 alpha 值。

getWidth(int id):获取指定资源 ID 对应的 Bitmap 图像的宽度。

getHeight(int id):获取指定资源 ID 对应的 Bitmap 图像的高度。

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

你可能感兴趣的:(androidbitmap)