Browse Source

Merge branch 'pdf'

Wayne 6 years ago
parent
commit
6a49a197b2

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

@@ -0,0 +1,51 @@
+package com.bomostory.sceneeditmodule
+
+import android.content.Context
+import android.graphics.*
+import com.example.tfat.myapplication.R
+
+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
+            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
 package com.bomostory.sceneeditmodule
 
 
 import android.content.Context
 import android.content.Context
-import android.graphics.*
 import com.bomostory.pdfexport.PdfBookWriter
 import com.bomostory.pdfexport.PdfBookWriter
 import com.bomostory.pdfexport.StandardPdfWriter
 import com.bomostory.pdfexport.StandardPdfWriter
 import com.bomostory.sceneeditmodule.basicdata.Project
 import com.bomostory.sceneeditmodule.basicdata.Project
-import com.example.tfat.myapplication.R
 import io.reactivex.Observable
 import io.reactivex.Observable
 import java.io.File
 import java.io.File
 
 
@@ -18,10 +16,10 @@ object PdfMaker {
             val height = 1080
             val height = 1080
             val coverWidth = width / 2
             val coverWidth = width / 2
             emitter.onNext(progress++ / total)
             emitter.onNext(progress++ / total)
-            val front = drawFrontCover(coverWidth, height, project.name
+            val front = CoverDrawer.drawFrontCover(coverWidth, height, project.name
                     ?: "", project.frontCoverColor.getColor(context))
                     ?: "", project.frontCoverColor.getColor(context))
             emitter.onNext(progress++ / total)
             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)
             emitter.onNext(progress++ / total)
             val standardPdfWriter = StandardPdfWriter(file, front, back)
             val standardPdfWriter = StandardPdfWriter(file, front, back)
             project.story?.scenes?.forEach {
             project.story?.scenes?.forEach {
@@ -44,10 +42,10 @@ object PdfMaker {
             val author = project?.author ?: ""
             val author = project?.author ?: ""
             emitter.onNext(progress++ / total)
             emitter.onNext(progress++ / total)
             /** front **/
             /** 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)
             emitter.onNext(progress++ / total)
             /** back **/
             /** 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)
             emitter.onNext(progress++ / total)
             /** pages **/
             /** pages **/
             val pdfBookWriter = PdfBookWriter(file, front, back)
             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)
-            }
-        }
-    }
 }
 }

+ 1 - 0
src/main/java/com/bomostory/sceneeditmodule/screen/movie/MovieEditActivity.kt

@@ -429,6 +429,7 @@ class MovieEditActivity : AppCompatActivity(),
 
 
     private fun onClickExportPdf() {
     private fun onClickExportPdf() {
         ExportPdfDialog().also { exportPdfDialog ->
         ExportPdfDialog().also { exportPdfDialog ->
+            exportPdfDialog.project = viewModel.project!!
             exportPdfDialog.image1Path = viewModel.project?.scene1File?.path ?: ""
             exportPdfDialog.image1Path = viewModel.project?.scene1File?.path ?: ""
             exportPdfDialog.image2Path = viewModel.project?.scene2File?.path ?: ""
             exportPdfDialog.image2Path = viewModel.project?.scene2File?.path ?: ""
             exportPdfDialog.onClickPrint = Runnable {
             exportPdfDialog.onClickPrint = Runnable {

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

@@ -1,13 +1,17 @@
 package com.bomostory.sceneeditmodule.share
 package com.bomostory.sceneeditmodule.share
 
 
+import android.content.Context
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
 import android.os.Bundle
 import android.os.Bundle
 import android.support.v4.app.DialogFragment
 import android.support.v4.app.DialogFragment
 import android.support.v4.app.FragmentManager
 import android.support.v4.app.FragmentManager
 import android.view.LayoutInflater
 import android.view.LayoutInflater
 import android.view.View
 import android.view.View
 import android.view.ViewGroup
 import android.view.ViewGroup
+import com.bomostory.sceneeditmodule.CoverDrawer
+import com.bomostory.sceneeditmodule.basicdata.Project
 import com.example.tfat.myapplication.R
 import com.example.tfat.myapplication.R
-import com.squareup.picasso.Picasso
 import kotlinx.android.synthetic.main.dialog_export_pdf.view.*
 import kotlinx.android.synthetic.main.dialog_export_pdf.view.*
 
 
 class ExportPdfDialog : DialogFragment() {
 class ExportPdfDialog : DialogFragment() {
@@ -18,6 +22,7 @@ class ExportPdfDialog : DialogFragment() {
         Booklet,
         Booklet,
     }
     }
 
 
+    lateinit var project: Project
     var image1Path = ""
     var image1Path = ""
     var image2Path = ""
     var image2Path = ""
     var image3Path = ""
     var image3Path = ""
@@ -41,18 +46,62 @@ class ExportPdfDialog : DialogFragment() {
             btn_exportPdfDialog_cancel.setOnClickListener { this@ExportPdfDialog.dismiss() }
             btn_exportPdfDialog_cancel.setOnClickListener { this@ExportPdfDialog.dismiss() }
             btn_exportPdfDialog_print.setOnClickListener { onClickPrint.run() }
             btn_exportPdfDialog_print.setOnClickListener { onClickPrint.run() }
             if (image1Path != "") {
             if (image1Path != "") {
+                val bitmap = decodeImage(context, image1Path)
+                iv_exportPdfDialog_standard_1.setImageBitmap(bitmap)
+                // Picasso not work, don't know why
+                /*
                 Picasso.get().load(image1Path).into(iv_exportPdfDialog_standard_1)
                 Picasso.get().load(image1Path).into(iv_exportPdfDialog_standard_1)
-                Picasso.get().load(image1Path).into(iv_exportPdfDialog_foldableBooklet_1)
+                */
             }
             }
             if (image2Path != "") {
             if (image2Path != "") {
+                val bitmap = decodeImage(context, image2Path)
+                iv_exportPdfDialog_standard_2.setImageBitmap(bitmap)
+                // Picasso not work, don't know why
+                /*
                 Picasso.get().load(image2Path).into(iv_exportPdfDialog_standard_2)
                 Picasso.get().load(image2Path).into(iv_exportPdfDialog_standard_2)
+                */
             }
             }
             if (image3Path != "") {
             if (image3Path != "") {
+                val bitmap = decodeImage(context, image3Path)
+                iv_exportPdfDialog_standard_3.setImageBitmap(bitmap)
+                // Picasso not work, don't know why
+                /*
                 Picasso.get().load(image3Path).into(iv_exportPdfDialog_standard_3)
                 Picasso.get().load(image3Path).into(iv_exportPdfDialog_standard_3)
+                */
             }
             }
+            iv_exportPdfDialog_foldableBookletCoverFront.setImageBitmap(createFrontCover(context))
+            iv_exportPdfDialog_foldableBookletCoverBack.setImageBitmap(createBackCover(context))
         }
         }
     }
     }
 
 
+    private fun createFrontCover(context: Context): Bitmap {
+        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 color = project.frontCoverColor.getColor(context)
+        return CoverDrawer.drawFrontCover(width, height, name, color)
+    }
+
+    private fun createBackCover(context: Context): Bitmap {
+        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 color = project.backCoverColor.getColor(context)
+        return CoverDrawer.drawBackCover(context, width, height, color)
+    }
+
+    private fun decodeImage(context: Context, imgPath: String): Bitmap {
+        val options = BitmapFactory.Options().apply {
+            inJustDecodeBounds = true
+        }
+        BitmapFactory.decodeFile(imgPath, options)
+        options.apply {
+            inJustDecodeBounds = false
+            val viewWidth = context.resources.getDimension(R.dimen.export_pdf_screenshot_width)
+            inSampleSize = (outWidth / viewWidth).toInt()
+        }
+        return BitmapFactory.decodeFile(imgPath, options)
+    }
+
     fun show(manager: FragmentManager?) {
     fun show(manager: FragmentManager?) {
         manager?.apply {
         manager?.apply {
             val ft = beginTransaction()
             val ft = beginTransaction()

+ 8 - 0
src/main/res/drawable/border_dotted_line_cooa_1dp.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+    <stroke
+        android:width="2dp"
+        android:color="@color/cocoa"
+        android:dashWidth="10px"
+        android:dashGap="10px" />
+</shape>

+ 24 - 11
src/main/res/layout/dialog_export_pdf.xml

@@ -52,8 +52,10 @@
 
 
     <ImageView
     <ImageView
         android:id="@+id/iv_exportPdfDialog_standard_1"
         android:id="@+id/iv_exportPdfDialog_standard_1"
-        android:layout_width="144dp"
-        android:layout_height="72dp"
+        android:layout_width="@dimen/export_pdf_screenshot_width"
+        android:layout_height="@dimen/export_pdf_screenshot_height"
+        android:background="#33AA00"
+        android:src="#111111"
         android:layout_marginTop="24dp"
         android:layout_marginTop="24dp"
         android:scaleType="centerCrop"
         android:scaleType="centerCrop"
         app:layout_constraintLeft_toLeftOf="@id/tv_exportPdfDialog_standard"
         app:layout_constraintLeft_toLeftOf="@id/tv_exportPdfDialog_standard"
@@ -61,8 +63,8 @@
 
 
     <ImageView
     <ImageView
         android:id="@+id/iv_exportPdfDialog_standard_2"
         android:id="@+id/iv_exportPdfDialog_standard_2"
-        android:layout_width="144dp"
-        android:layout_height="72dp"
+        android:layout_width="@dimen/export_pdf_screenshot_width"
+        android:layout_height="@dimen/export_pdf_screenshot_height"
         android:layout_marginLeft="16dp"
         android:layout_marginLeft="16dp"
         android:scaleType="centerCrop"
         android:scaleType="centerCrop"
         app:layout_constraintLeft_toRightOf="@id/iv_exportPdfDialog_standard_1"
         app:layout_constraintLeft_toRightOf="@id/iv_exportPdfDialog_standard_1"
@@ -70,8 +72,8 @@
 
 
     <ImageView
     <ImageView
         android:id="@+id/iv_exportPdfDialog_standard_3"
         android:id="@+id/iv_exportPdfDialog_standard_3"
-        android:layout_width="144dp"
-        android:layout_height="72dp"
+        android:layout_width="@dimen/export_pdf_screenshot_width"
+        android:layout_height="@dimen/export_pdf_screenshot_height"
         android:layout_marginLeft="16dp"
         android:layout_marginLeft="16dp"
         android:scaleType="centerCrop"
         android:scaleType="centerCrop"
         app:layout_constraintLeft_toRightOf="@id/iv_exportPdfDialog_standard_2"
         app:layout_constraintLeft_toRightOf="@id/iv_exportPdfDialog_standard_2"
@@ -100,14 +102,25 @@
         app:layout_constraintTop_toTopOf="@id/rb_exportPdfDialog_foldableBooklet" />
         app:layout_constraintTop_toTopOf="@id/rb_exportPdfDialog_foldableBooklet" />
 
 
     <ImageView
     <ImageView
-        android:id="@+id/iv_exportPdfDialog_foldableBooklet_1"
-        android:layout_width="144dp"
-        android:layout_height="72dp"
+        android:id="@+id/iv_exportPdfDialog_foldableBookletCoverFront"
+        android:layout_width="@dimen/export_pdf_cover_width"
+        android:foreground="@drawable/border_dotted_line_cooa_1dp"
+        android:layout_height="@dimen/export_pdf_screenshot_height"
         android:layout_marginTop="24dp"
         android:layout_marginTop="24dp"
         android:scaleType="centerCrop"
         android:scaleType="centerCrop"
         app:layout_constraintLeft_toLeftOf="@id/tv_exportPdfDialog_foldableBooklet"
         app:layout_constraintLeft_toLeftOf="@id/tv_exportPdfDialog_foldableBooklet"
         app:layout_constraintTop_toBottomOf="@id/tv_exportPdfDialog_foldableBooklet" />
         app:layout_constraintTop_toBottomOf="@id/tv_exportPdfDialog_foldableBooklet" />
 
 
+    <ImageView
+        android:id="@+id/iv_exportPdfDialog_foldableBookletCoverBack"
+        android:layout_width="@dimen/export_pdf_cover_width"
+        android:layout_height="@dimen/export_pdf_screenshot_height"
+        android:foreground="@drawable/border_dotted_line_cooa_1dp"
+        android:scaleType="centerCrop"
+        app:layout_constraintBottom_toBottomOf="@id/iv_exportPdfDialog_foldableBookletCoverFront"
+        app:layout_constraintLeft_toRightOf="@id/iv_exportPdfDialog_foldableBookletCoverFront"
+        app:layout_constraintTop_toTopOf="@id/iv_exportPdfDialog_foldableBookletCoverFront" />
+
     <ImageView
     <ImageView
         android:id="@+id/iv_exportPdfDialog_foldableBooklet_2"
         android:id="@+id/iv_exportPdfDialog_foldableBooklet_2"
         android:layout_width="488dp"
         android:layout_width="488dp"
@@ -116,9 +129,9 @@
         android:layout_marginRight="40dp"
         android:layout_marginRight="40dp"
         android:scaleType="centerCrop"
         android:scaleType="centerCrop"
         android:src="@drawable/ic_img_foldable"
         android:src="@drawable/ic_img_foldable"
-        app:layout_constraintLeft_toRightOf="@id/iv_exportPdfDialog_foldableBooklet_1"
+        app:layout_constraintLeft_toRightOf="@id/iv_exportPdfDialog_foldableBookletCoverBack"
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintRight_toRightOf="parent"
-        app:layout_constraintTop_toTopOf="@id/iv_exportPdfDialog_foldableBooklet_1" />
+        app:layout_constraintTop_toTopOf="@id/iv_exportPdfDialog_foldableBookletCoverBack" />
     <Button
     <Button
         android:id="@+id/btn_exportPdfDialog_print"
         android:id="@+id/btn_exportPdfDialog_print"
         style="@style/Widget.AppCompat.Button.Borderless.Colored"
         style="@style/Widget.AppCompat.Button.Borderless.Colored"

+ 5 - 0
src/main/res/values/dimens.xml

@@ -14,4 +14,9 @@
 
 
     <dimen name="cover_editor_dialog_width">800dp</dimen>
     <dimen name="cover_editor_dialog_width">800dp</dimen>
     <dimen name="cover_editor_dialog_height">544dp</dimen>
     <dimen name="cover_editor_dialog_height">544dp</dimen>
+
+    <dimen name="export_pdf_screenshot_width">144dp</dimen>
+    <dimen name="export_pdf_screenshot_height">72dp</dimen>
+    <dimen name="export_pdf_cover_width">72dp</dimen>
+    <dimen name="export_pdf_cover_height">72dp</dimen>
 </resources>
 </resources>