二维码海报文本修复

This commit is contained in:
2026-06-02 17:55:48 +08:00
parent 5822e562de
commit b1aa844ff6

View File

@@ -60,7 +60,7 @@ class InvitationCodeActivity : AppCompatActivity() {
.init() .init()
initListener() initListener()
// 默认显示时效为24h // 默认显示时效为 24h
binding.tvExpireTime.text = getString(R.string.expire_24h) binding.tvExpireTime.text = getString(R.string.expire_24h)
// 默认显示灰色占位区域,二维码不可见 // 默认显示灰色占位区域,二维码不可见
hideQrCode() hideQrCode()
@@ -190,7 +190,13 @@ class InvitationCodeActivity : AppCompatActivity() {
android.util.Log.d("InvitationCode", "推荐码: $generatedInviteCode, 过期时间戳: $expireTimestamp") android.util.Log.d("InvitationCode", "推荐码: $generatedInviteCode, 过期时间戳: $expireTimestamp")
return qrCodeContent return qrCodeContent
} }
/*
* 这段代码生成带Logo的二维码
1. 使用ZXing库编码内容生成二维码矩阵
2. 遍历矩阵绘制黑白像素点
3. 在中心位置叠加应用Logo图标
4. 返回合成的Bitmap失败返回null
*/
private fun createQRCodeWithLogo(content: String, width: Int, height: Int): Bitmap? { private fun createQRCodeWithLogo(content: String, width: Int, height: Int): Bitmap? {
return try { return try {
val hints = Hashtable<EncodeHintType, Any>() val hints = Hashtable<EncodeHintType, Any>()
@@ -247,6 +253,13 @@ class InvitationCodeActivity : AppCompatActivity() {
} }
} }
/*
* 这段代码实现了分享到微信的功能:
1. 检查二维码是否过期
2. 生成分享海报图片
3. 保存图片到本地
4. 调用系统分享功能发送图片到微信
* */
private fun shareToWeChat() { private fun shareToWeChat() {
if (expireTimestamp == 0L || System.currentTimeMillis() > expireTimestamp) { if (expireTimestamp == 0L || System.currentTimeMillis() > expireTimestamp) {
ToastUtils.showError(this, getString(R.string.qr_code_expired)) ToastUtils.showError(this, getString(R.string.qr_code_expired))
@@ -267,7 +280,14 @@ class InvitationCodeActivity : AppCompatActivity() {
shareImage(imagePath) shareImage(imagePath)
} }
/*
这段代码生成分享海报图片:
1. 创建背景画布
2. 生成并绘制二维码到指定位置
3. 根据过期时间动态生成提示文字
4. 使用StaticLayout绘制多行文本居中对齐
5. 返回合成的Bitmap图片
*/
private fun generateSharePoster(): Bitmap? { private fun generateSharePoster(): Bitmap? {
return try { return try {
val posterBackground = ContextCompat.getDrawable(this, R.drawable.share_poster_empty) val posterBackground = ContextCompat.getDrawable(this, R.drawable.share_poster_empty)
@@ -302,28 +322,25 @@ class InvitationCodeActivity : AppCompatActivity() {
color = Color.parseColor("#0475F3") color = Color.parseColor("#0475F3")
textSize = 36f textSize = 36f
isAntiAlias = true isAntiAlias = true
textAlign = Paint.Align.CENTER
isFakeBoldText = false isFakeBoldText = false
} }
val horizontalPadding = (width * 0.12f).toInt() val horizontalPadding = (width * 0.20f).toInt()
val textWidth = width - horizontalPadding * 2 - (width * 0.08f).toInt() val textWidth = width - horizontalPadding * 2
val staticLayout = android.text.StaticLayout.Builder.obtain( val staticLayout = android.text.StaticLayout.Builder.obtain(
tipsText, 0, tipsText.length, tipsPaint, textWidth tipsText, 0, tipsText.length, tipsPaint, textWidth
) )
.setAlignment(android.text.Layout.Alignment.ALIGN_CENTER) .setAlignment(android.text.Layout.Alignment.ALIGN_NORMAL)
.setLineSpacing(0f, 1.4f) .setLineSpacing(0f, 1f)
.build() .build()
val qrBottom = qrY + qrSize val qrBottom = qrY + qrSize
val marginBelowQr = (height * 0.04f).toInt() val marginBelowQr = (height * 0.03f).toInt()
val textY = qrBottom + marginBelowQr val textY = qrBottom + marginBelowQr
val rightOffset = (width * 0.35f).toInt()
canvas.save() canvas.save()
canvas.translate((horizontalPadding + rightOffset).toFloat(), textY.toFloat()) canvas.translate(horizontalPadding.toFloat(), textY.toFloat())
staticLayout.draw(canvas) staticLayout.draw(canvas)
canvas.restore() canvas.restore()
@@ -333,7 +350,13 @@ class InvitationCodeActivity : AppCompatActivity() {
null null
} }
} }
/*
* 这段代码将Bitmap保存到文件
* 1. 获取应用外部图片存储目录
* 2. 创建以时间戳命名的PNG文件
* 3. 压缩并写入图片数据
* 4. 返回文件绝对路径,失败返回空字符串
*/
private fun saveBitmapToFile(bitmap: Bitmap): String { private fun saveBitmapToFile(bitmap: Bitmap): String {
return try { return try {
val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES) val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
@@ -350,7 +373,14 @@ class InvitationCodeActivity : AppCompatActivity() {
"" ""
} }
} }
/*
* 这段代码实现图片分享功能:
1. 检查图片文件是否存在
2. 通过FileProvider生成Content URI
3. 构建分享Intent设置图片和文本
4. 授予目标应用读取权限
5. 启动系统分享选择器
* */
private fun shareImage(imagePath: String) { private fun shareImage(imagePath: String) {
val file = File(imagePath) val file = File(imagePath)
if (!file.exists()) { if (!file.exists()) {