Browse Source

Refactor : Extract draw cover function to CoverDrawer

Wayne 6 years ago
parent
commit
84e9b84cab

+ 48 - 0
src/main/java/com/bomostory/sceneeditmodule/CoverDrawer.kt

@@ -0,0 +1,48 @@
+package com.bomostory.sceneeditmodule
+
+import android.content.Context
+import android.graphics.*
+import com.example.tfat.myapplication.R
+
+object CoverDrawer {
+    fun drawFrontCover(width: Int, height: Int, name: String, coverColor: Int): Bitmap {
+        val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
+            color = Color.WHITE
+            textSize = 100f
+            textAlign = Paint.Align.CENTER
+            flags += Paint.FAKE_BOLD_TEXT_FLAG
+        }
+        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)
+            }
+        }
+    }
+
+    fun drawBackCover(context: Context, width: Int, height: Int, coverColor: Int): Bitmap {
+        return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444).apply {
+            Canvas(this).apply {
+                drawColor(coverColor)
+                val drawable = context.getDrawable(R.drawable.ic_logo_full_white)
+                val marginBottom = this.height / 10
+                val w = this.width / 3
+                val scale = w.toFloat() / drawable.intrinsicWidth.toFloat()
+                val h = drawable.intrinsicHeight * scale
+                val left = (this.width - w) / 2
+                val top = (this.height - h) - marginBottom
+                val right = (this.width + w) / 2
+                val bottom = (this.height) - marginBottom
+                drawable.setBounds(left, top.toInt(), right, bottom)
+                drawable.draw(this)
+            }
+        }
+    }
+}

+ 4 - 46
src/main/java/com/bomostory/sceneeditmodule/PdfMaker.kt

@@ -1,11 +1,9 @@
 package com.bomostory.sceneeditmodule
 
 import android.content.Context
-import android.graphics.*
 import com.bomostory.pdfexport.PdfBookWriter
 import com.bomostory.pdfexport.StandardPdfWriter
 import com.bomostory.sceneeditmodule.basicdata.Project
-import com.example.tfat.myapplication.R
 import io.reactivex.Observable
 import java.io.File
 
@@ -18,10 +16,10 @@ object PdfMaker {
             val height = 1080
             val coverWidth = width / 2
             emitter.onNext(progress++ / total)
-            val front = drawFrontCover(coverWidth, height, project.name
+            val front = CoverDrawer.drawFrontCover(coverWidth, height, project.name
                     ?: "", project.frontCoverColor.getColor(context))
             emitter.onNext(progress++ / total)
-            val back = drawBackCover(context, coverWidth, height, project.backCoverColor.getColor(context))
+            val back = CoverDrawer.drawBackCover(context, coverWidth, height, project.backCoverColor.getColor(context))
             emitter.onNext(progress++ / total)
             val standardPdfWriter = StandardPdfWriter(file, front, back)
             project.story?.scenes?.forEach {
@@ -44,10 +42,10 @@ object PdfMaker {
             val author = project?.author ?: ""
             emitter.onNext(progress++ / total)
             /** front **/
-            val front = drawFrontCover(width, height, name, project.frontCoverColor.getColor(context))
+            val front = CoverDrawer.drawFrontCover(width, height, name, project.frontCoverColor.getColor(context))
             emitter.onNext(progress++ / total)
             /** back **/
-            val back = drawBackCover(context, width, height, project.backCoverColor.getColor(context))
+            val back = CoverDrawer.drawBackCover(context, width, height, project.backCoverColor.getColor(context))
             emitter.onNext(progress++ / total)
             /** pages **/
             val pdfBookWriter = PdfBookWriter(file, front, back)
@@ -61,44 +59,4 @@ object PdfMaker {
         }
     }
 
-    private fun drawFrontCover(width: Int, height: Int, name: String, coverColor: Int): Bitmap {
-        val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
-            color = Color.WHITE
-            textSize = 100f
-            textAlign = Paint.Align.CENTER
-            flags += Paint.FAKE_BOLD_TEXT_FLAG
-        }
-        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)
-            }
-        }
-    }
-
-    private fun drawBackCover(context: Context, width: Int, height: Int, coverColor: Int): Bitmap {
-        return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444).apply {
-            Canvas(this).apply {
-                drawColor(coverColor)
-                val drawable = context.getDrawable(R.drawable.ic_logo_full_white)
-                val marginBottom = this.height / 10
-                val w = this.width / 3
-                val scale = w.toFloat() / drawable.intrinsicWidth.toFloat()
-                val h = drawable.intrinsicHeight * scale
-                val left = (this.width - w) / 2
-                val top = (this.height - h) - marginBottom
-                val right = (this.width + w) / 2
-                val bottom = (this.height) - marginBottom
-                drawable.setBounds(left, top.toInt(), right, bottom)
-                drawable.draw(this)
-            }
-        }
-    }
 }