Android Bitmap.createBitmap() 用法全解析

在 Android 开发中,Bitmap.createBitmap() 是一个非常强大的方法,可以用来 创建新的位图,并且可以 进行旋转、缩放、平移、镜像翻转、裁剪等操作。本篇文章将详细介绍 Bitmap.createBitmap() 的各种用法。


1. Bitmap.createBitmap() 基础用法

1.1 创建一个空白 Bitmap

1
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
  • width:Bitmap 的宽度。
  • height:Bitmap 的高度。
  • Bitmap.Config.ARGB_8888:像素格式,支持透明度。

用途:用于创建空白的 Bitmap,然后通过 Canvas 进行绘制。


2. 旋转(Rotate)

如果拍照后的图片方向不正确,可以使用 Matrix 进行旋转。

1
2
3
4
5
fun rotateBitmap(bitmap: Bitmap, degree: Int): Bitmap {
val matrix = Matrix()
matrix.postRotate(degree.toFloat()) // 旋转 degree 度
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}
  • postRotate(degree):将图片旋转指定角度。

示例:旋转 90 度

1
val rotatedBitmap = rotateBitmap(originalBitmap, 90)

3. 缩放(Scale)

如果想要调整图片大小,可以使用 postScale()

1
2
3
4
5
fun scaleBitmap(bitmap: Bitmap, sx: Float, sy: Float): Bitmap {
val matrix = Matrix()
matrix.postScale(sx, sy) // 设置缩放比例
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}

示例:缩小 50%

1
val scaledBitmap = scaleBitmap(originalBitmap, 0.5f, 0.5f)

4. 平移(Translate)

可以使用 postTranslate() 来移动 Bitmap 位置。

1
2
3
4
5
fun translateBitmap(bitmap: Bitmap, dx: Float, dy: Float): Bitmap {
val matrix = Matrix()
matrix.postTranslate(dx, dy) // X 方向右移 dx,Y 方向下移 dy
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}

示例:右移 50px,下移 100px

1
val translatedBitmap = translateBitmap(originalBitmap, 50f, 100f)

5. 镜像翻转(Flip)

5.1 左右翻转(水平镜像)

1
2
3
4
5
fun flipHorizontal(bitmap: Bitmap): Bitmap {
val matrix = Matrix()
matrix.postScale(-1f, 1f) // 水平方向镜像
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}

5.2 上下翻转(垂直镜像)

1
2
3
4
5
fun flipVertical(bitmap: Bitmap): Bitmap {
val matrix = Matrix()
matrix.postScale(1f, -1f) // 垂直方向镜像
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}

示例:左右翻转

1
val flippedBitmap = flipHorizontal(originalBitmap)

6. 裁剪(Crop)

如果只想截取 Bitmap 的一部分,可以使用:

1
2
3
fun cropBitmap(bitmap: Bitmap, x: Int, y: Int, width: Int, height: Int): Bitmap {
return Bitmap.createBitmap(bitmap, x, y, width, height)
}

示例:从 (50,50) 开始,裁剪 200x200 区域

1
val croppedBitmap = cropBitmap(originalBitmap, 50, 50, 200, 200)

7. 综合变换:旋转 + 缩放 + 平移

可以组合多个变换,比如 先旋转 45°,再缩小 80%,然后平移

1
2
3
4
5
6
7
fun transformBitmap(bitmap: Bitmap): Bitmap {
val matrix = Matrix()
matrix.postRotate(45f) // 旋转 45 度
matrix.postScale(0.8f, 0.8f) // 缩小 80%
matrix.postTranslate(100f, 50f) // 右移 100,下移 50
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}

示例:应用组合变换

1
val transformedBitmap = transformBitmap(originalBitmap)

8. 总结

操作 方法 示例
创建空白 Bitmap Bitmap.createBitmap(width, height, config) Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888)
旋转 postRotate(degree) postRotate(90f)
缩放 postScale(sx, sy) postScale(0.5f, 0.5f)
平移 postTranslate(dx, dy) postTranslate(50f, 100f)
水平翻转 postScale(-1f, 1f) postScale(-1f, 1f)
垂直翻转 postScale(1f, -1f) postScale(1f, -1f)
裁剪 Bitmap.createBitmap(bitmap, x, y, w, h) Bitmap.createBitmap(bitmap, 50, 50, 200, 200)

📌 结论:

Bitmap.createBitmap() 不仅能创建 Bitmap,还能旋转、缩放、平移、镜像翻转、裁剪,几乎所有图片变换都可以搞定! 🚀

希望这篇文章能帮你更好地理解 Bitmap.createBitmap() 的使用! 😊