From d59d55575d913646b7a90fca651904ab889c6723 Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期五, 18 二月 2022 16:30:53 +0800
Subject: [PATCH] ### 一、任务模块相关

---
 app/src/main/java/cn/flightfeather/thirdappmodule/util/photo/ImageMergeUtil.kt |   67 +++++++++++++++++++++++++++++++--
 1 files changed, 62 insertions(+), 5 deletions(-)

diff --git a/app/src/main/java/cn/flightfeather/thirdappmodule/util/photo/ImageMergeUtil.kt b/app/src/main/java/cn/flightfeather/thirdappmodule/util/photo/ImageMergeUtil.kt
index d1a6a81..8fae237 100644
--- a/app/src/main/java/cn/flightfeather/thirdappmodule/util/photo/ImageMergeUtil.kt
+++ b/app/src/main/java/cn/flightfeather/thirdappmodule/util/photo/ImageMergeUtil.kt
@@ -6,12 +6,15 @@
 import android.os.Build
 import android.support.annotation.ColorInt
 import android.util.DisplayMetrics
+import android.widget.TextView
 import cn.flightfeather.thirdappmodule.R
 import cn.flightfeather.thirdappmodule.util.DateFormatter
 import cn.flightfeather.thirdappmodule.util.dp
 import cn.flightfeather.thirdappmodule.util.sp
+import org.jetbrains.anko.backgroundColor
 import java.util.*
 import java.util.regex.Pattern
+import kotlin.math.abs
 import kotlin.math.max
 import kotlin.math.roundToInt
 
@@ -27,6 +30,7 @@
     private val bgWidth:Float
     private val bgHeight: Float
     private val textPaint:Paint
+    private val textPaint2:Paint
     private val indexPaint:Paint
     private val titlePaint:Paint
 
@@ -51,6 +55,14 @@
             isAntiAlias = true
             textSize = 14f.sp
         }
+        textPaint2 = Paint().apply {
+            textAlign = Paint.Align.LEFT
+            color = Color.WHITE
+            style = Paint.Style.FILL_AND_STROKE
+//            strokeWidth = density * 2
+            isAntiAlias = true
+            textSize = 14f.sp
+        }
         indexPaint = Paint().apply {
             textAlign = Paint.Align.CENTER
             color = Color.BLACK
@@ -67,6 +79,24 @@
             textSize = 14f.sp
             typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD)
         }
+    }
+
+    /**
+     * 缁欏浘鐗囨坊鍔犳按鍗�
+     * 榛樿姘村嵃
+     */
+    fun addLogo(bitmap: Bitmap, logo: Bitmap): Bitmap {
+        // 榛樿姘村嵃瀹藉害涓哄浘鐗囩殑 1/8
+        val newLogo = resizeBitmap(logo, bitmap.width.toFloat() / 7)
+        val newBitmap = Bitmap.createBitmap(bitmap.width, bitmap.height, bitmap.config)
+        val canvas = Canvas(newBitmap)
+        canvas.drawBitmap(bitmap, 0f, 0f, null)
+        val margin = 24f// 璁惧畾logo鍜屽浘鐗囩殑杈硅窛
+        val x = bitmap.width - newLogo.width - margin
+        val y = margin
+        canvas.drawBitmap(newLogo, x, y, null)
+
+        return newBitmap
     }
 
     fun mergeImage(bitmaps: MutableList<Bitmap>): Bitmap? {
@@ -324,12 +354,39 @@
         return newBitmap
     }
 
-    fun mergeText(bitmap1: Bitmap, text: String, coordinate: Pair<Float, Float>): Bitmap {
-        val result = Bitmap.createBitmap(bitmap1.width, bitmap1.height, Bitmap.Config.ARGB_8888)
-        val canvas = Canvas(result)
+    fun mergeText(bitmap1: Bitmap, textView: TextView, coordinateO: Pair<Float, Float>, xScale: Float, yScale: Float): Bitmap {
+        textPaint2.textSize = textView.textSize
+        var coordinate = coordinateO
+        var textMaxWidth = bgWidth - coordinate.first * 2
+        /* 鏍规嵁灞忓箷澶у皬閲嶇粯鏂扮殑鍥剧墖瀹介珮锛岀‘淇濇枃鏈瓧浣撴樉绀轰笉浼氳缂╁皬 */
+        val scaledBitmap = if (bitmap1.width > bgWidth) {
+            val y = coordinate.second / bgHeight * bitmap1.height
+            coordinate = Pair(coordinate.first, y)
+            textPaint2.textSize = textPaint2.textSize / bgHeight * bitmap1.height
+            textMaxWidth = bitmap1.width - coordinate.first * 2
+//            resizeBitmap(bitmap1, bgWidth)
+            Bitmap.createBitmap(bitmap1.width, bitmap1.height, bitmap1.config)
+        } else {
+            Bitmap.createBitmap(bitmap1.width, bitmap1.height, bitmap1.config)
+        }
+
+        val fontMetrics = textPaint2.fontMetrics
+        val textHeight = abs(fontMetrics.bottom) + abs(fontMetrics.ascent)
+        /* 鏂囨湰鎹㈣ */
+        val textArray = linefeed(textView.text.toString(), textMaxWidth, textPaint2)
+        val canvas = Canvas(scaledBitmap)
         canvas.drawBitmap(bitmap1, 0F, 0F, null)
-        canvas.drawText(text, coordinate.first, coordinate.second, null)
-        return result
+
+        var y = scaledBitmap.height * yScale + abs(fontMetrics.top)
+        val x = scaledBitmap.width * xScale
+        textArray.forEach {
+            textPaint2.color = Color.BLACK
+            canvas.drawText(it, x + 3, y + 3, textPaint2)
+            textPaint2.color = Color.WHITE
+            canvas.drawText(it, x, y, textPaint2)
+            y += textHeight
+        }
+        return scaledBitmap
     }
 
     fun createTitle(text: String, time: Date?): Bitmap {

--
Gitblit v1.9.3