feat: 二维码添加圆角效果
使用 Canvas + PorterDuff 裁剪,6%圆角半径 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -86,10 +86,11 @@ class BindFragment : BaseFragment<FragmentBindBinding>() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 将 ZXing BitMatrix 转换为 Android Bitmap */
|
/** 将 ZXing BitMatrix 转换为圆角 Android Bitmap */
|
||||||
private fun bitMatrixToBitmap(matrix: BitMatrix): Bitmap {
|
private fun bitMatrixToBitmap(matrix: BitMatrix): Bitmap {
|
||||||
val width = matrix.width
|
val width = matrix.width
|
||||||
val height = matrix.height
|
val height = matrix.height
|
||||||
|
// 先生成方形 Bitmap
|
||||||
val pixels = IntArray(width * height)
|
val pixels = IntArray(width * height)
|
||||||
for (y in 0 until height) {
|
for (y in 0 until height) {
|
||||||
for (x in 0 until width) {
|
for (x in 0 until width) {
|
||||||
@@ -100,7 +101,20 @@ class BindFragment : BaseFragment<FragmentBindBinding>() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888)
|
val squareBitmap = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888)
|
||||||
|
|
||||||
|
// 绘制圆角
|
||||||
|
val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
|
||||||
|
val canvas = android.graphics.Canvas(output)
|
||||||
|
val paint = android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG)
|
||||||
|
val rect = android.graphics.RectF(0f, 0f, width.toFloat(), height.toFloat())
|
||||||
|
val radius = width * 0.06f // 6% 圆角
|
||||||
|
canvas.drawRoundRect(rect, radius, radius, paint)
|
||||||
|
paint.xfermode = android.graphics.PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN)
|
||||||
|
canvas.drawBitmap(squareBitmap, 0f, 0f, paint)
|
||||||
|
squareBitmap.recycle()
|
||||||
|
|
||||||
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user