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