步骤:1创建一个类(任何名称)
class Utils {
companion object {
fun createRoundedRectBitmap(
bitmap: Bitmap,
topLeftCorner: Float, topRightCorner: Float,
bottomRightCorner: Float, bottomLeftCorner: Float
): Bitmap? {
val output = Bitmap.createBitmap(
bitmap.width, bitmap.height,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(output)
val color = Color.WHITE
val paint = Paint()
val rect = Rect(0, 0, bitmap.width, bitmap.height)
val rectF = RectF(rect)
val path = Path()
val radii = floatArrayOf(
topLeftCorner, topLeftCorner,
topRightCorner, topRightCorner,
bottomRightCorner, bottomRightCorner,
bottomLeftCorner, bottomLeftCorner
)
paint.isAntiAlias = true
canvas.drawARGB(0, 0, 0, 0)
paint.color = color
path.addRoundRect(rectF, radii, Path.Direction.CW)
canvas.drawPath(path, paint)
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
canvas.drawBitmap(bitmap, rect, rect, paint)
return output
}
}
}
步骤:2你可以这样得到位图
val bitmap = AppCompatResources.getDrawable(this, R.drawable.imgItem)?.toBitmap()
步骤:3
binding.imgItem.setImageBitmap(bitmap?.let {
ProgressUtils.loadBitmap(
it, 50.toFloat(),
50.toFloat(),
20.toFloat(),
8.toFloat()
)
})