Browse Source

Merge branch 'cover'

Wayne 6 years ago
parent
commit
3fad05c845

+ 26 - 0
src/androidTest/java/com/bomostory/sceneeditmodule/CoverDrawerTest.kt

@@ -0,0 +1,26 @@
+package com.bomostory.sceneeditmodule
+
+import android.support.test.rule.GrantPermissionRule
+import org.junit.Test
+
+import org.junit.Assert.*
+import org.junit.Rule
+import java.io.File
+
+class CoverDrawerTest {
+
+    @get:Rule
+    var permissionRule = GrantPermissionRule.grant(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
+
+    @Test
+    fun drawFrontCover() {
+
+        val w = 360 * 10
+        val h = 360 * 10
+        val title = "This is fucking good story"
+        val author = "Wayne"
+        val coverFile = File("")
+        val bitmap = CoverDrawer.drawFrontCover(w, h, title, author, coverFile)
+        print("")
+    }
+}

+ 54 - 14
src/main/java/com/bomostory/sceneeditmodule/CoverDrawer.kt

@@ -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)
             }
         }
     }

+ 6 - 5
src/main/java/com/bomostory/sceneeditmodule/PdfMaker.kt

@@ -16,8 +16,9 @@ object PdfMaker {
             val height = 1080
             val coverWidth = width / 2
             emitter.onNext(progress++ / total)
-            val front = CoverDrawer.drawFrontCover(coverWidth, height, project.name
-                    ?: "", project.frontCoverColor.getColor(context))
+            val name = project.name ?: ""
+            val author = project.author ?: ""
+            val front = CoverDrawer.drawFrontCover(coverWidth, height, name, author, project.frontCoverColor.getColor(context))
             emitter.onNext(progress++ / total)
             val back = CoverDrawer.drawBackCover(context, coverWidth, height, project.backCoverColor.getColor(context))
             emitter.onNext(progress++ / total)
@@ -38,11 +39,11 @@ object PdfMaker {
             var progress = 0
             val width = 1920 / 2
             val height = 1080
-            val name = project?.name ?: ""
-            val author = project?.author ?: ""
+            val name = project.name ?: ""
+            val author = project.author ?: ""
             emitter.onNext(progress++ / total)
             /** front **/
-            val front = CoverDrawer.drawFrontCover(width, height, name, project.frontCoverColor.getColor(context))
+            val front = CoverDrawer.drawFrontCover(width, height, name, author, project.frontCoverColor.getColor(context))
             emitter.onNext(progress++ / total)
             /** back **/
             val back = CoverDrawer.drawBackCover(context, width, height, project.backCoverColor.getColor(context))

+ 2 - 1
src/main/java/com/bomostory/sceneeditmodule/share/ExportPdfDialog.kt

@@ -78,8 +78,9 @@ class ExportPdfDialog : DialogFragment() {
         val width = context.resources.getDimension(R.dimen.export_pdf_cover_width).toInt()
         val height = context.resources.getDimension(R.dimen.export_pdf_cover_height).toInt()
         val name = project.name ?: ""
+        val author = project.author ?: ""
         val color = project.frontCoverColor.getColor(context)
-        return CoverDrawer.drawFrontCover(width, height, name, color)
+        return CoverDrawer.drawFrontCover(width, height, name, author, color)
     }
 
     private fun createBackCover(context: Context): Bitmap {