位图¶
BmpUtils
主要为你提供了关于 Bitmap 的一些方法
位图合并¶
下面的示例使用纯色 Bitmap
作为演示。
internal fun getColorBitmap(@ColorInt colorInt: Int, width: Int, height: Int) =
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).also {
it.eraseColor(colorInt)
}
上下重叠¶
调用 mergeBitmap
可以将两个位图上下重叠合并在一起,合并后的大小由底部位图决定。
val mergeBitmap = BmpUtils.mergeBitmap(
getColorBitmap(Color(0xFF6ab04c).toArgb(),200,200),
getColorBitmap(Color(0xFFeb4d4b).toArgb(),300,300)
)
目前 mergeBitmap
提供五种位置可供选择。
左右拼接¶
调用 mergeBitmapLR
可以将两个位图左右拼接合并在一起,你可以决定是小图放大还是大图缩小。
val mergeBitmap = BmpUtils.mergeBitmapLR(
getColorBitmap(Color(0xFF6ab04c).toArgb(), 200, 200),
getColorBitmap(Color(0xFFeb4d4b).toArgb(), 300, 300),
MergeScale.BIG_REDUCE
)
参数 | 示例图 |
---|---|
MergeScale.SMALL_ENLARGE | |
MergeScale.BIG_REDUCE |
上下拼接¶
调用 mergeBitmapTB
可以将两个位图上下拼接合并在一起,你可以决定是小图放大还是大图缩小。
val mergeBitmap = BmpUtils.mergeBitmapTB(
getColorBitmap(Color(0xFF6ab04c).toArgb(), 200, 200),
getColorBitmap(Color(0xFFeb4d4b).toArgb(), 300, 300),
MergeScale.SMALL_ENLARGE
)
参数 | 示例图 | 参数 | 示例 |
---|---|---|---|
MergeScale.SMALL_ENLARGE | MergeScale.BIG_REDUCE |
通过 Base64 获取位图¶
通过 getBitmapFromBase64
方法将 base64字符串转换成 Bitmap
对象。
val base64 = .....
val base64Bitmap = BmpUtils.getBitmapFromBase64(base64)
通过 Drawable 获取位图¶
getBitmapFromDrawable
可以将 Drawable
转换成 Bitmap
对象。
val context = LocalContext.current
val bitmap = BmpUtils.getBitmapFromDrawable(R.drawable.ic_github, context)
Image(bitmap = bitmap.asImageBitmap(), contentDescription = "测试图片")
位图缩放¶
如下图所示,使用 scaleBitmap
可以将位图放大到你需要的尺寸。
val size = 50F.DP.toInt()
val requiredSize = 100F.DP.toInt()
val bitmap = BmpUtils.scaleBitmap(
getColorBitmap(Color(0xFF6ab04c).toArgb(), size, size),
requiredSize, requiredSize
)
获取尺寸¶
getBitmapWidthHeight
允许你传入对应的解码方式并返回解码后的位图尺寸。
val context = LocalContext.current
val (width, height) = BmpUtils.getBitmapWidthHeight { options ->
BitmapFactory.decodeResource(context.resources, R.drawable.github, options)
}