| | |
| | | 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 |
| | | |
| | |
| | | private val bgWidth:Float |
| | | private val bgHeight: Float |
| | | private val textPaint:Paint |
| | | private val textPaint2:Paint |
| | | private val indexPaint:Paint |
| | | private val titlePaint:Paint |
| | | |
| | |
| | | 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 |
| | |
| | | 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? { |
| | |
| | | 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 { |