|
@@ -2,30 +2,70 @@ package com.bomostory.sceneeditmodule
|
|
|
|
|
|
import android.content.Context
|
|
|
import android.graphics.*
|
|
|
+import android.text.TextPaint
|
|
|
+import android.text.TextUtils
|
|
|
import com.example.tfat.myapplication.R
|
|
|
+import java.io.File
|
|
|
|
|
|
object CoverDrawer {
|
|
|
- private const val DEFAULT_HEIGHT = 1080f
|
|
|
|
|
|
- fun drawFrontCover(width: Int, height: Int, name: String, coverColor: Int): Bitmap {
|
|
|
- val scale = height.toFloat() / DEFAULT_HEIGHT
|
|
|
- val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
|
- color = Color.WHITE
|
|
|
- textSize = 100f * scale
|
|
|
+ fun drawFrontCover(width: Int, height: Int, name: String, author: String, coverFile: File): Bitmap {
|
|
|
+ return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444).apply {
|
|
|
+ Canvas(this).apply {
|
|
|
+ if (coverFile.exists()) {
|
|
|
+ val cover = BitmapFactory.decodeFile(coverFile.absolutePath)
|
|
|
+ drawBitmap(cover, 0f, 0f, null)
|
|
|
+ }
|
|
|
+ drawMastTileAuthor(name, author)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun Canvas.drawMastTileAuthor(title: String, author: String) {
|
|
|
+ val maskMarginTop = height * (232f / 360f)
|
|
|
+ val maskMarginBottom = height * (344f / 360f)
|
|
|
+ val titleMarginTop = height * ((232f + 16f) / 360f)
|
|
|
+ val titleHeight = height * (47f / 360f)
|
|
|
+ val titleSize = height * (40f / 360f)
|
|
|
+ val authorHeight = height * (24f / 360f)
|
|
|
+ val authorMarginTop = height * ((232f + 16f + 47f + 9f) / 360f)
|
|
|
+ val authorSize = height * (20f / 360f)
|
|
|
+
|
|
|
+ val maskRect = RectF(0f, maskMarginTop, width.toFloat(), maskMarginBottom)
|
|
|
+ val maskPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
|
+ color = Color.parseColor("#66ffffff")
|
|
|
+ style = Paint.Style.FILL_AND_STROKE
|
|
|
+ }
|
|
|
+ drawRect(maskRect, maskPaint)
|
|
|
+
|
|
|
+ val titlePaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
|
+ color = Color.BLACK
|
|
|
+ textSize = titleSize
|
|
|
+ textAlign = Paint.Align.CENTER
|
|
|
+ flags += Paint.FAKE_BOLD_TEXT_FLAG
|
|
|
+ }
|
|
|
+ val titleText = TextUtils.ellipsize(title, titlePaint, width.toFloat(), TextUtils.TruncateAt.END)
|
|
|
+ val titleX = width / 2f
|
|
|
+ val titleY = titleMarginTop + titleHeight
|
|
|
+ drawText(titleText, 0, titleText.length, titleX, titleY, titlePaint)
|
|
|
+
|
|
|
+ val authorPaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
|
+ color = Color.BLACK
|
|
|
+ textSize = authorSize
|
|
|
textAlign = Paint.Align.CENTER
|
|
|
flags += Paint.FAKE_BOLD_TEXT_FLAG
|
|
|
}
|
|
|
+ val authorText = TextUtils.ellipsize(author, authorPaint, width.toFloat(), TextUtils.TruncateAt.END)
|
|
|
+ val authorX = width / 2f
|
|
|
+ val authorY = authorMarginTop + authorHeight
|
|
|
+ drawText(authorText, 0, authorText.length, authorX, authorY, authorPaint)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun drawFrontCover(width: Int, height: Int, name: String, author: String, coverColor: Int): Bitmap {
|
|
|
return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444).apply {
|
|
|
Canvas(this).apply {
|
|
|
drawColor(coverColor)
|
|
|
- val marginBottom = height / 20
|
|
|
- val rect = Rect(0, height - height / 3 - marginBottom, width, height - marginBottom)
|
|
|
- val p = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
|
- color = Color.parseColor("#66ffffff")
|
|
|
- style = Paint.Style.FILL_AND_STROKE
|
|
|
- }
|
|
|
- drawRect(rect, p)
|
|
|
- drawText(name, rect.centerX().toFloat(), rect.centerY().toFloat(), paint)
|
|
|
+ drawMastTileAuthor(name, author)
|
|
|
}
|
|
|
}
|
|
|
}
|