(一) Bitmap 概述
Bitmap 是 Android 系统中的图像处理的最重要类之一。
用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。
Bitmap 是一种用来表示图像的数据结构。
它是一个二维的像素数组,用来存储图像的像素信息,包括颜色值、宽度、高度等。
Bitmap 可以从资源文件、网络、文件等来源加载图像数据,并可以在应用程序中进行操作,比如裁剪、缩放、旋转等。
Bitmap 在 Android开发中被广泛应用于显示图片、图标、背景等。
由于 Bitmap 占用内存较大,开发者需要注意及时释放不再使用的 Bitmap 对象,以避免内存泄漏问题。
Bitmap 相关的使用主要有两种:
1.给 ImageView 设置背景
2.当做画布来使用
1 | imageView.setImageBitmap(Bitmap bm); |
在Android开发中,Bitmap类是用于表示位图图像的对象,常用方法包括:
createBitmap()
:创建一个新的Bitmap对象。1
val bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)
decodeResource()
:从资源文件中解码一个 Bitmap对象。1
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.image)
compress()
:压缩Bitmap对象。
将数据压缩到该输出流中1
2val outputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)getWidth()
和getHeight()
:获取Bitmap对象的宽度和高度。1
2val width = bitmap.width
val height = bitmap.heightgetPixel()
和setPixel()
:获取和设置Bitmap对象的像素值。1
2val pixel = bitmap.getPixel(10, 10)
bitmap.setPixel(10, 10, Color.RED)copy()
:复制一个Bitmap对象。1
val copiedBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
recycle()
:释放Bitmap对象占用的内存。1
bitmap.recycle()
(二) Bitmap的格式
我们知道Bitmap是位图,是由像素点组成的,这就涉及到两个问题,
第一:如何存储每个像素点?
第二:怎么压缩像素点?
Bitmap 中有两个内部枚举类:Config 和 CompressFormat
- Config 是用来设置颜色配置信息的
- CompressFormat 是用来设置压缩方式的
(1) Bitmap 存储格式
Config | 单位像素所占字节数 | 解释 |
---|---|---|
Bitmap.Config.ALPHA_8 | 1 | 颜色信息只由透明度组成,占8位 |
Bitmap.Config.ARGB_4444 | 2 | 颜色信息由rgba四部分组成,每个部分都占4位,总共占16位 |
Bitmap.Config.ARGB_8888 | 4 | 颜色信息由rgba四部分组成,每个部分都占8位,总共占32位。是Bitmap默认的颜色配置信息,也是最占空间的一种配置 |
Bitmap.Config.RGB_565 | 2 | 颜色信息由rgb三部分组成,R占5位,G占6位,B占5位,总共占16位 |
RGBA_F16 | 8 | Android 8.0新增 (更丰富的色彩表现HDR) |
HARDWARE | Special | Android 8.0 新增(Bitmap直接存储在graphic memory) |
注意以下三点即可:
- 一般情况下用 ARGB_8888 格式存储 Bitmap
- ARGB_4444 画面惨不忍睹,被弃用
- 假如对图片没有透明度要求,可以使用 RGB_565,比 ARGB_8888 节省一半的内存开销
(2) 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 对象压缩成指定格式和质量的图像数据,并写入到指定文件名的文件中。
压缩格式
压缩格式使用枚举类 Bitmap.CompressFormat
压缩方式 | 解释 |
---|---|
Bitmap.CompressFormat.JPEG | 表示以 JPEG 压缩算法进行图像压缩,压缩后的格式可以是”jpg”或者”jpeg”是一种有损压缩 |
Bitmap.CompressFormat.PNG | 表示以 PNG 压缩算法进行图像压缩,压缩后的格式可以是”.png”,是一种无损压缩 |
Bitmap.CompressFormat.WEBP | 表示以 WebP 压缩算法进行图像压缩,压缩后的格式可以是”webp”,是一种有损压缩,质量相同的情况下, WebP 格式图像的体积要比 JPEG格式图像小40%。美中不足的是, WebP 格式图像的编码时间“比JPEG格式图像长8倍 |
(三) Bitmap 创建方法
(1) 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 对象中,同时指定是否可变。
(1) Bitmap 的静态方法 createBitmap()
常见的传参方式包括:
- 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 对象
(2) BitmapFactory 的 decode… 系列静态方法
BitmapFactory 类提供了一系列用于解码位图的静态方法,常见的包括:
- decodeResource(Resources res, int resId):根据指定的资源 ID 创建一个 Bitmap 对象,用于加载应用程序资源文件中的图片。
- decodeFile(String pathName):根据文件路径解码一个 Bitmap 对象。
- decodeStream(InputStream is):根据输入流解码一个 Bitmap 对象。
- decodeByteArray(byte[] data, int offset, int length):根据字节数组解码一个 Bitmap 对象。
- decodeFileDescriptor(FileDescriptor fd):根据文件描述符解码一个 Bitmap 对象。
- decodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts):根据资源ID和输入流解码一个Bitmap对象。