|
@@ -0,0 +1,146 @@
|
|
|
+package com.kdanmobile.reader.screen
|
|
|
+
|
|
|
+import android.support.v7.app.AppCompatActivity
|
|
|
+import android.os.Bundle
|
|
|
+import android.view.MenuItem
|
|
|
+import android.view.View
|
|
|
+import android.widget.SeekBar
|
|
|
+import com.kdanmobile.reader.R
|
|
|
+import com.kdanmobile.reader.screen.view.ColorSelectView
|
|
|
+import com.kdanmobile.reader.screen.view.edit.SignatureDrawView
|
|
|
+import kotlinx.android.synthetic.main.activity_view_signature_create.*
|
|
|
+import android.graphics.Bitmap
|
|
|
+import android.support.v4.content.ContextCompat
|
|
|
+import com.kdanmobile.reader.Config
|
|
|
+import com.kdanmobile.reader.screen.view.edit.OnSignatureAddListener
|
|
|
+import java.io.File
|
|
|
+import java.io.FileOutputStream
|
|
|
+import java.io.IOException
|
|
|
+
|
|
|
+class SignatureActivity : AppCompatActivity() {
|
|
|
+
|
|
|
+ companion object {
|
|
|
+ var signatureAddNotifyManager: OnSignatureAddListener? = null
|
|
|
+ }
|
|
|
+
|
|
|
+ private var onSignatureAddListener: OnSignatureAddListener? = null
|
|
|
+ private var noSignature = true
|
|
|
+ set(value) {
|
|
|
+ field = value
|
|
|
+ when (field) {
|
|
|
+ true -> {
|
|
|
+ tv_writeHere.visibility = View.VISIBLE
|
|
|
+ btn_save_signature.setTextColor(ContextCompat.getColor(baseContext, android.R.color.darker_gray))
|
|
|
+ btn_save_signature.isClickable = false
|
|
|
+ }
|
|
|
+ false -> {
|
|
|
+ tv_writeHere.visibility = View.INVISIBLE
|
|
|
+ btn_save_signature.setTextColor(ContextCompat.getColor(baseContext, android.R.color.white))
|
|
|
+ btn_save_signature.isClickable = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
+ super.onCreate(savedInstanceState)
|
|
|
+ setContentView(R.layout.activity_view_signature_create)
|
|
|
+
|
|
|
+ onSignatureAddListener = signatureAddNotifyManager
|
|
|
+ signatureAddNotifyManager = null
|
|
|
+
|
|
|
+ setupToolbar()
|
|
|
+
|
|
|
+ colorChooser_signature.onColorSelectedListener = object : ColorSelectView.OnColorSelectedListener {
|
|
|
+ override fun onColorSelected(color: Int) {
|
|
|
+ drawView.paintColor = color
|
|
|
+ }
|
|
|
+ }
|
|
|
+ drawView.paintColor = colorChooser_signature.getSelectedColor()
|
|
|
+
|
|
|
+ seekBar_signature.onSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener {
|
|
|
+ override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
|
|
|
+ drawView.strokeWidth = seekBar_signature.currentValue.toFloat()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onStartTrackingTouch(seekBar: SeekBar?) {}
|
|
|
+
|
|
|
+ override fun onStopTrackingTouch(seekBar: SeekBar?) {
|
|
|
+ drawView.strokeWidth = seekBar_signature.currentValue.toFloat()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ drawView.strokeWidth = seekBar_signature.currentValue.toFloat()
|
|
|
+
|
|
|
+ btn_save_signature.setOnClickListener {
|
|
|
+ if (!noSignature) {
|
|
|
+ val bitmap = drawView.getBitmap()
|
|
|
+
|
|
|
+ val argb = IntArray(bitmap.width * bitmap.height)
|
|
|
+ bitmap.getPixels(argb, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height)
|
|
|
+ var minX = bitmap.width
|
|
|
+ var maxX = 0
|
|
|
+ var minY = bitmap.height
|
|
|
+ var maxY = 0
|
|
|
+ var index = 0
|
|
|
+ for (y in 0 until bitmap.height) {
|
|
|
+ for (x in 0 until bitmap.width) {
|
|
|
+ val alpha = (argb[index] shr 24) and 0xff
|
|
|
+ if (alpha > 10) {
|
|
|
+ minX = Math.min(x, minX)
|
|
|
+ maxX = Math.max(x, maxX)
|
|
|
+ minY = Math.min(y, minY)
|
|
|
+ maxY = Math.max(y, maxY)
|
|
|
+ }
|
|
|
+ index++
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ val cropBitmap = Bitmap.createBitmap(bitmap, minX, minY, maxX - minX + 1, maxY - minY + 1)
|
|
|
+
|
|
|
+ val path = Config.getSignatureDirectoryPath(baseContext)
|
|
|
+ val dir = File(path)
|
|
|
+ if (!dir.exists() || !dir.isDirectory) {
|
|
|
+ dir.mkdirs()
|
|
|
+ }
|
|
|
+ val filename = "$path${File.separator}Signature${System.currentTimeMillis()}.png"
|
|
|
+ try {
|
|
|
+ val fileOutputStream = FileOutputStream(filename)
|
|
|
+ cropBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)
|
|
|
+ fileOutputStream.close()
|
|
|
+ } catch (e: IOException) {
|
|
|
+ e.printStackTrace()
|
|
|
+ }
|
|
|
+
|
|
|
+ onSignatureAddListener?.onSignatureAdd(filename)
|
|
|
+ finish()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ btn_clear_signature.setOnClickListener {
|
|
|
+ drawView.clear()
|
|
|
+ }
|
|
|
+
|
|
|
+ drawView.onSignatureDrawListener = object : SignatureDrawView.OnSignatureDrawListener {
|
|
|
+ override fun onClearSignature() {
|
|
|
+ noSignature = true
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onStartSignature() {
|
|
|
+ noSignature = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ noSignature = true
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun setupToolbar() {
|
|
|
+ setSupportActionBar(toolbar_signature)
|
|
|
+ supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_close)
|
|
|
+ supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onOptionsItemSelected(item: MenuItem?): Boolean {
|
|
|
+ when (item?.itemId) {
|
|
|
+ android.R.id.home -> finish()
|
|
|
+ }
|
|
|
+ return super.onOptionsItemSelected(item)
|
|
|
+ }
|
|
|
+}
|