misc updates here and there
This commit is contained in:
parent
f0d2c8e491
commit
1052af3e42
|
@ -14,12 +14,12 @@ import com.simplemobiletools.commons.extensions.getContrastColor
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
||||||
private val mPaint: Paint
|
var mPaths: MutableMap<MyPath, PaintOptions>
|
||||||
private var mPath: MyPath? = null
|
private var mPaint: Paint
|
||||||
private var mPaths: MutableMap<MyPath, PaintOptions>? = null
|
private var mPath: MyPath
|
||||||
private var mListener: PathsChangedListener? = null
|
private var mPaintOptions: PaintOptions
|
||||||
|
|
||||||
private var mPaintOptions: PaintOptions? = null
|
private var mListener: PathsChangedListener? = null
|
||||||
private var mCurX = 0f
|
private var mCurX = 0f
|
||||||
private var mCurY = 0f
|
private var mCurY = 0f
|
||||||
private var mStartX = 0f
|
private var mStartX = 0f
|
||||||
|
@ -28,19 +28,19 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
||||||
private var mIsStrokeWidthBarEnabled = false
|
private var mIsStrokeWidthBarEnabled = false
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
|
||||||
mPath = MyPath()
|
mPath = MyPath()
|
||||||
mPaint = Paint()
|
|
||||||
mPaintOptions = PaintOptions()
|
mPaintOptions = PaintOptions()
|
||||||
mPaint.color = mPaintOptions!!.color
|
mPaint = Paint().apply {
|
||||||
mPaint.style = Paint.Style.STROKE
|
color = mPaintOptions.color
|
||||||
mPaint.strokeJoin = Paint.Join.ROUND
|
style = Paint.Style.STROKE
|
||||||
mPaint.strokeCap = Paint.Cap.ROUND
|
strokeJoin = Paint.Join.ROUND
|
||||||
mPaint.strokeWidth = mPaintOptions!!.strokeWidth
|
strokeCap = Paint.Cap.ROUND
|
||||||
mPaint.isAntiAlias = true
|
strokeWidth = mPaintOptions.strokeWidth
|
||||||
|
isAntiAlias = true
|
||||||
|
}
|
||||||
|
|
||||||
mPaths = LinkedHashMap<MyPath, PaintOptions>()
|
mPaths = LinkedHashMap<MyPath, PaintOptions>()
|
||||||
mPaths!!.put(mPath!!, mPaintOptions!!)
|
mPaths.put(mPath, mPaintOptions)
|
||||||
pathsUpdated()
|
pathsUpdated()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,28 +49,25 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun undo() {
|
fun undo() {
|
||||||
if (mPaths!!.isEmpty())
|
if (mPaths.isEmpty())
|
||||||
return
|
return
|
||||||
|
|
||||||
var lastKey: MyPath? = null
|
val lastKey: MyPath? = mPaths.keys.lastOrNull()
|
||||||
for (key in mPaths!!.keys) {
|
|
||||||
lastKey = key
|
|
||||||
}
|
|
||||||
|
|
||||||
mPaths!!.remove(lastKey)
|
mPaths.remove(lastKey)
|
||||||
pathsUpdated()
|
pathsUpdated()
|
||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setColor(newColor: Int) {
|
fun setColor(newColor: Int) {
|
||||||
mPaintOptions!!.color = newColor
|
mPaintOptions.color = newColor
|
||||||
if (mIsStrokeWidthBarEnabled) {
|
if (mIsStrokeWidthBarEnabled) {
|
||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setStrokeWidth(newStrokeWidth: Float) {
|
fun setStrokeWidth(newStrokeWidth: Float) {
|
||||||
mPaintOptions!!.strokeWidth = newStrokeWidth
|
mPaintOptions.strokeWidth = newStrokeWidth
|
||||||
if (mIsStrokeWidthBarEnabled) {
|
if (mIsStrokeWidthBarEnabled) {
|
||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
|
@ -81,35 +78,31 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
val bitmap: Bitmap
|
fun getBitmap(): Bitmap {
|
||||||
get() {
|
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
|
||||||
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
|
val canvas = Canvas(bitmap)
|
||||||
val canvas = Canvas(bitmap)
|
canvas.drawColor(Color.WHITE)
|
||||||
canvas.drawColor(Color.WHITE)
|
mIsSaving = true
|
||||||
mIsSaving = true
|
draw(canvas)
|
||||||
draw(canvas)
|
mIsSaving = false
|
||||||
mIsSaving = false
|
return bitmap
|
||||||
return bitmap
|
}
|
||||||
}
|
|
||||||
|
|
||||||
val paths: Map<MyPath, PaintOptions>
|
|
||||||
get() = mPaths!!
|
|
||||||
|
|
||||||
fun addPath(path: MyPath, options: PaintOptions) {
|
fun addPath(path: MyPath, options: PaintOptions) {
|
||||||
mPaths!!.put(path, options)
|
mPaths.put(path, options)
|
||||||
pathsUpdated()
|
pathsUpdated()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDraw(canvas: Canvas) {
|
override fun onDraw(canvas: Canvas) {
|
||||||
super.onDraw(canvas)
|
super.onDraw(canvas)
|
||||||
|
|
||||||
for ((key, value) in mPaths!!) {
|
for ((key, value) in mPaths) {
|
||||||
changePaint(value)
|
changePaint(value)
|
||||||
canvas.drawPath(key, mPaint)
|
canvas.drawPath(key, mPaint)
|
||||||
}
|
}
|
||||||
|
|
||||||
changePaint(mPaintOptions!!)
|
changePaint(mPaintOptions)
|
||||||
canvas.drawPath(mPath!!, mPaint)
|
canvas.drawPath(mPath, mPaint)
|
||||||
|
|
||||||
if (mIsStrokeWidthBarEnabled && !mIsSaving) {
|
if (mIsStrokeWidthBarEnabled && !mIsSaving) {
|
||||||
drawPreviewCircle(canvas)
|
drawPreviewCircle(canvas)
|
||||||
|
@ -121,15 +114,15 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
||||||
mPaint.style = Paint.Style.FILL
|
mPaint.style = Paint.Style.FILL
|
||||||
|
|
||||||
var y = height - res.getDimension(R.dimen.preview_dot_offset_y)
|
var y = height - res.getDimension(R.dimen.preview_dot_offset_y)
|
||||||
canvas.drawCircle((width / 2).toFloat(), y, mPaintOptions!!.strokeWidth / 2, mPaint)
|
canvas.drawCircle((width / 2).toFloat(), y, mPaintOptions.strokeWidth / 2, mPaint)
|
||||||
mPaint.style = Paint.Style.STROKE
|
mPaint.style = Paint.Style.STROKE
|
||||||
mPaint.color = mPaintOptions!!.color.getContrastColor()
|
mPaint.color = mPaintOptions.color.getContrastColor()
|
||||||
mPaint.strokeWidth = res.getDimension(R.dimen.preview_dot_stroke_size)
|
mPaint.strokeWidth = res.getDimension(R.dimen.preview_dot_stroke_size)
|
||||||
|
|
||||||
y = height - res.getDimension(R.dimen.preview_dot_offset_y)
|
y = height - res.getDimension(R.dimen.preview_dot_offset_y)
|
||||||
val radius = (mPaintOptions!!.strokeWidth + res.getDimension(R.dimen.preview_dot_stroke_size)) / 2
|
val radius = (mPaintOptions.strokeWidth + res.getDimension(R.dimen.preview_dot_stroke_size)) / 2
|
||||||
canvas.drawCircle((width / 2).toFloat(), y, radius, mPaint)
|
canvas.drawCircle((width / 2).toFloat(), y, radius, mPaint)
|
||||||
changePaint(mPaintOptions!!)
|
changePaint(mPaintOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun changePaint(paintOptions: PaintOptions) {
|
private fun changePaint(paintOptions: PaintOptions) {
|
||||||
|
@ -138,45 +131,43 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clearCanvas() {
|
fun clearCanvas() {
|
||||||
mPath!!.reset()
|
mPath.reset()
|
||||||
mPaths!!.clear()
|
mPaths.clear()
|
||||||
pathsUpdated()
|
pathsUpdated()
|
||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun actionDown(x: Float, y: Float) {
|
private fun actionDown(x: Float, y: Float) {
|
||||||
mPath!!.reset()
|
mPath.reset()
|
||||||
mPath!!.moveTo(x, y)
|
mPath.moveTo(x, y)
|
||||||
mCurX = x
|
mCurX = x
|
||||||
mCurY = y
|
mCurY = y
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun actionMove(x: Float, y: Float) {
|
private fun actionMove(x: Float, y: Float) {
|
||||||
mPath!!.quadTo(mCurX, mCurY, (x + mCurX) / 2, (y + mCurY) / 2)
|
mPath.quadTo(mCurX, mCurY, (x + mCurX) / 2, (y + mCurY) / 2)
|
||||||
mCurX = x
|
mCurX = x
|
||||||
mCurY = y
|
mCurY = y
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun actionUp() {
|
private fun actionUp() {
|
||||||
mPath!!.lineTo(mCurX, mCurY)
|
mPath.lineTo(mCurX, mCurY)
|
||||||
|
|
||||||
// draw a dot on click
|
// draw a dot on click
|
||||||
if (mStartX == mCurX && mStartY == mCurY) {
|
if (mStartX == mCurX && mStartY == mCurY) {
|
||||||
mPath!!.lineTo(mCurX, mCurY + 2)
|
mPath.lineTo(mCurX, mCurY + 2)
|
||||||
mPath!!.lineTo(mCurX + 1, mCurY + 2)
|
mPath.lineTo(mCurX + 1, mCurY + 2)
|
||||||
mPath!!.lineTo(mCurX + 1, mCurY)
|
mPath.lineTo(mCurX + 1, mCurY)
|
||||||
}
|
}
|
||||||
|
|
||||||
mPaths!!.put(mPath!!, mPaintOptions!!)
|
mPaths.put(mPath, mPaintOptions)
|
||||||
pathsUpdated()
|
pathsUpdated()
|
||||||
mPath = MyPath()
|
mPath = MyPath()
|
||||||
mPaintOptions = PaintOptions(mPaintOptions!!.color, mPaintOptions!!.strokeWidth)
|
mPaintOptions = PaintOptions(mPaintOptions.color, mPaintOptions.strokeWidth)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun pathsUpdated() {
|
private fun pathsUpdated() {
|
||||||
if (mListener != null && mPaths != null) {
|
mListener?.pathsChanged(mPaths.size)
|
||||||
mListener!!.pathsChanged(mPaths!!.size)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onTouchEvent(event: MotionEvent): Boolean {
|
override fun onTouchEvent(event: MotionEvent): Boolean {
|
||||||
|
@ -207,7 +198,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
||||||
val superState = super.onSaveInstanceState()
|
val superState = super.onSaveInstanceState()
|
||||||
val savedState = SavedState(superState)
|
val savedState = SavedState(superState)
|
||||||
|
|
||||||
savedState.mPaths = mPaths
|
savedState.paths = mPaths
|
||||||
return savedState
|
return savedState
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,12 +210,12 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
||||||
val savedState = state
|
val savedState = state
|
||||||
super.onRestoreInstanceState(savedState.superState)
|
super.onRestoreInstanceState(savedState.superState)
|
||||||
|
|
||||||
mPaths = savedState.mPaths
|
mPaths = savedState.paths
|
||||||
pathsUpdated()
|
pathsUpdated()
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class SavedState : View.BaseSavedState {
|
internal class SavedState : View.BaseSavedState {
|
||||||
var mPaths: MutableMap<MyPath, PaintOptions>? = null
|
var paths: MutableMap<MyPath, PaintOptions> = HashMap()
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val CREATOR: Parcelable.Creator<SavedState> = object : Parcelable.Creator<SavedState> {
|
val CREATOR: Parcelable.Creator<SavedState> = object : Parcelable.Creator<SavedState> {
|
||||||
|
@ -238,8 +229,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
||||||
|
|
||||||
override fun writeToParcel(out: Parcel, flags: Int) {
|
override fun writeToParcel(out: Parcel, flags: Int) {
|
||||||
super.writeToParcel(out, flags)
|
super.writeToParcel(out, flags)
|
||||||
out.writeInt(mPaths!!.size)
|
out.writeInt(paths.size)
|
||||||
for ((key, paintOptions) in mPaths!!) {
|
for ((key, paintOptions) in paths) {
|
||||||
out.writeSerializable(key)
|
out.writeSerializable(key)
|
||||||
out.writeInt(paintOptions.color)
|
out.writeInt(paintOptions.color)
|
||||||
out.writeFloat(paintOptions.strokeWidth)
|
out.writeFloat(paintOptions.strokeWidth)
|
||||||
|
@ -251,7 +242,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
||||||
for (i in 0..size - 1) {
|
for (i in 0..size - 1) {
|
||||||
val key = parcel.readSerializable() as MyPath
|
val key = parcel.readSerializable() as MyPath
|
||||||
val paintOptions = PaintOptions(parcel.readInt(), parcel.readFloat())
|
val paintOptions = PaintOptions(parcel.readInt(), parcel.readFloat())
|
||||||
mPaths!!.put(key, paintOptions)
|
paths.put(key, paintOptions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ object Svg {
|
||||||
|
|
||||||
val out = FileOutputStream(output)
|
val out = FileOutputStream(output)
|
||||||
val writer = BufferedWriter(OutputStreamWriter(out))
|
val writer = BufferedWriter(OutputStreamWriter(out))
|
||||||
writeSvg(writer, backgroundColor, canvas.paths, canvas.width, canvas.height)
|
writeSvg(writer, backgroundColor, canvas.mPaths, canvas.width, canvas.height)
|
||||||
writer.close()
|
writer.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ object Svg {
|
||||||
writer.write("\" height=\"")
|
writer.write("\" height=\"")
|
||||||
writer.write(height.toString())
|
writer.write(height.toString())
|
||||||
writer.write("\" fill=\"#")
|
writer.write("\" fill=\"#")
|
||||||
writer.write(Integer.toHexString(backgroundColor).substring(2)) // Skip the alpha FF
|
writer.write(Integer.toHexString(backgroundColor).substring(2))
|
||||||
writer.write("\"/>")
|
writer.write("\"/>")
|
||||||
|
|
||||||
for ((key, value) in paths) {
|
for ((key, value) in paths) {
|
||||||
|
@ -50,7 +50,7 @@ object Svg {
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.write("\" fill=\"none\" stroke=\"#")
|
writer.write("\" fill=\"none\" stroke=\"#")
|
||||||
writer.write(Integer.toHexString(options.color).substring(2)) // Skip the alpha FF
|
writer.write(Integer.toHexString(options.color).substring(2))
|
||||||
writer.write("\" stroke-width=\"")
|
writer.write("\" stroke-width=\"")
|
||||||
writer.write(options.strokeWidth.toString())
|
writer.write(options.strokeWidth.toString())
|
||||||
writer.write("\" stroke-linecap=\"round\"/>")
|
writer.write("\" stroke-linecap=\"round\"/>")
|
||||||
|
@ -115,11 +115,11 @@ object Svg {
|
||||||
return svg
|
return svg
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SSvg internal constructor() : Serializable {
|
private class SSvg : Serializable {
|
||||||
internal var width: Int = 0
|
var background: SRect? = null
|
||||||
internal var height: Int = 0
|
val paths: ArrayList<SPath> = ArrayList()
|
||||||
internal var background: SRect? = null
|
private var width = 0
|
||||||
internal val paths: ArrayList<SPath> = ArrayList()
|
private var height = 0
|
||||||
|
|
||||||
internal fun setSize(w: Int, h: Int) {
|
internal fun setSize(w: Int, h: Int) {
|
||||||
width = w
|
width = w
|
||||||
|
@ -127,7 +127,7 @@ object Svg {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SRect internal constructor(internal val width: Int, internal val height: Int, internal val color: Int) : Serializable
|
private class SRect(val width: Int, val height: Int, val color: Int) : Serializable
|
||||||
|
|
||||||
private class SPath internal constructor(internal var data: String, internal var color: Int, internal var strokeWidth: Float) : Serializable
|
private class SPath(var data: String, var color: Int, var strokeWidth: Float) : Serializable
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,19 +10,18 @@ import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.support.v4.app.ActivityCompat
|
import android.support.v4.app.ActivityCompat
|
||||||
import android.support.v4.content.ContextCompat
|
|
||||||
import android.support.v4.content.FileProvider
|
import android.support.v4.content.FileProvider
|
||||||
import android.support.v7.app.AlertDialog
|
import android.support.v7.app.AlertDialog
|
||||||
import android.util.Log
|
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
import android.widget.RadioGroup
|
import android.widget.RadioGroup
|
||||||
import android.widget.SeekBar
|
import android.widget.SeekBar
|
||||||
import android.widget.Toast
|
|
||||||
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
|
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
|
||||||
import com.simplemobiletools.commons.extensions.beVisibleIf
|
import com.simplemobiletools.commons.extensions.beVisibleIf
|
||||||
|
import com.simplemobiletools.commons.extensions.hasWriteStoragePermission
|
||||||
import com.simplemobiletools.commons.extensions.toast
|
import com.simplemobiletools.commons.extensions.toast
|
||||||
|
import com.simplemobiletools.commons.extensions.value
|
||||||
import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN
|
import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN
|
||||||
import com.simplemobiletools.draw.BuildConfig
|
import com.simplemobiletools.draw.BuildConfig
|
||||||
import com.simplemobiletools.draw.MyCanvas
|
import com.simplemobiletools.draw.MyCanvas
|
||||||
|
@ -43,7 +42,6 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
|
||||||
private var strokeWidth = 0f
|
private var strokeWidth = 0f
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val TAG = MainActivity::class.java.simpleName
|
|
||||||
private val FOLDER_NAME = "images"
|
private val FOLDER_NAME = "images"
|
||||||
private val FILE_NAME = "simple-draw.png"
|
private val FILE_NAME = "simple-draw.png"
|
||||||
private val SAVE_FOLDER_NAME = "Simple Draw"
|
private val SAVE_FOLDER_NAME = "Simple Draw"
|
||||||
|
@ -64,7 +62,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
|
||||||
stroke_width_bar.progress = strokeWidth.toInt()
|
stroke_width_bar.progress = strokeWidth.toInt()
|
||||||
|
|
||||||
color_picker.setOnClickListener { pickColor() }
|
color_picker.setOnClickListener { pickColor() }
|
||||||
undo.setOnClickListener { undo() }
|
undo.setOnClickListener { my_canvas.undo() }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
|
@ -80,11 +78,6 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
|
||||||
config.brushSize = strokeWidth
|
config.brushSize = strokeWidth
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
super.onDestroy()
|
|
||||||
config.isFirstRun = false
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
menuInflater.inflate(R.menu.menu, menu)
|
menuInflater.inflate(R.menu.menu, menu)
|
||||||
return true
|
return true
|
||||||
|
@ -92,36 +85,15 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
|
||||||
|
|
||||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
when (item.itemId) {
|
when (item.itemId) {
|
||||||
R.id.menu_save -> {
|
R.id.menu_save -> saveImage()
|
||||||
saveImage()
|
R.id.menu_share -> shareImage()
|
||||||
return true
|
R.id.clear -> my_canvas.clearCanvas()
|
||||||
}
|
R.id.change_background -> changeBackgroundClicked()
|
||||||
R.id.menu_share -> {
|
R.id.settings -> launchSettings()
|
||||||
shareImage()
|
R.id.about -> launchAbout()
|
||||||
return true
|
|
||||||
}
|
|
||||||
R.id.settings -> {
|
|
||||||
startActivity(Intent(applicationContext, SettingsActivity::class.java))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
R.id.clear -> {
|
|
||||||
my_canvas.clearCanvas()
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
R.id.change_background -> {
|
|
||||||
val oldColor = (my_canvas.background as ColorDrawable).color
|
|
||||||
ColorPickerDialog(this, oldColor) {
|
|
||||||
setBackgroundColor(it)
|
|
||||||
config.canvasBackgroundColor = it
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
R.id.about -> {
|
|
||||||
launchAbout()
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
else -> return super.onOptionsItemSelected(item)
|
else -> return super.onOptionsItemSelected(item)
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
||||||
|
@ -131,17 +103,29 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
|
||||||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
saveImage()
|
saveImage()
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this, resources.getString(R.string.no_permissions), Toast.LENGTH_SHORT).show()
|
toast(R.string.no_permissions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun launchSettings() {
|
||||||
|
startActivity(Intent(applicationContext, SettingsActivity::class.java))
|
||||||
|
}
|
||||||
|
|
||||||
private fun launchAbout() {
|
private fun launchAbout() {
|
||||||
startAboutActivity(R.string.app_name, LICENSE_KOTLIN, BuildConfig.VERSION_NAME)
|
startAboutActivity(R.string.app_name, LICENSE_KOTLIN, BuildConfig.VERSION_NAME)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun changeBackgroundClicked() {
|
||||||
|
val oldColor = (my_canvas.background as ColorDrawable).color
|
||||||
|
ColorPickerDialog(this, oldColor) {
|
||||||
|
setBackgroundColor(it)
|
||||||
|
config.canvasBackgroundColor = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun saveImage() {
|
private fun saveImage() {
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
if (!hasWriteStoragePermission()) {
|
||||||
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), STORAGE_PERMISSION)
|
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), STORAGE_PERMISSION)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -163,28 +147,28 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
|
||||||
builder.setPositiveButton(R.string.ok, null)
|
builder.setPositiveButton(R.string.ok, null)
|
||||||
builder.setNegativeButton(R.string.cancel, null)
|
builder.setNegativeButton(R.string.cancel, null)
|
||||||
|
|
||||||
val alertDialog = builder.create()
|
builder.create().apply {
|
||||||
alertDialog.show()
|
show()
|
||||||
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
|
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
|
||||||
val fileName = fileNameET.text.toString().trim { it <= ' ' }
|
val fileName = fileNameET.value
|
||||||
if (!fileName.isEmpty()) {
|
if (!fileName.isEmpty()) {
|
||||||
val extension: String
|
val extension = when (fileExtensionRG.checkedRadioButtonId) {
|
||||||
when (fileExtensionRG.checkedRadioButtonId) {
|
R.id.extension_radio_svg -> ".svg"
|
||||||
R.id.extension_radio_svg -> extension = ".svg"
|
else -> ".png"
|
||||||
else -> extension = ".png"
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (saveFile(fileName, extension)) {
|
if (saveFile(fileName, extension)) {
|
||||||
curFileName = fileName
|
curFileName = fileName
|
||||||
curExtensionId = fileExtensionRG.checkedRadioButtonId
|
curExtensionId = fileExtensionRG.checkedRadioButtonId
|
||||||
|
|
||||||
toast(R.string.saving_ok)
|
toast(R.string.saving_ok)
|
||||||
alertDialog.dismiss()
|
dismiss()
|
||||||
|
} else {
|
||||||
|
toast(R.string.saving_error)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
toast(R.string.saving_error)
|
toast(R.string.enter_file_name)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
toast(R.string.enter_file_name)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -201,20 +185,17 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
|
||||||
val file = File(directory, fileName + extension)
|
val file = File(directory, fileName + extension)
|
||||||
when (extension) {
|
when (extension) {
|
||||||
".png" -> {
|
".png" -> {
|
||||||
val bitmap = my_canvas.bitmap
|
|
||||||
var out: FileOutputStream? = null
|
var out: FileOutputStream? = null
|
||||||
try {
|
try {
|
||||||
out = FileOutputStream(file)
|
out = FileOutputStream(file)
|
||||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
|
my_canvas.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out)
|
||||||
MediaScannerConnection.scanFile(applicationContext, arrayOf(file.absolutePath), null, null)
|
MediaScannerConnection.scanFile(applicationContext, arrayOf(file.absolutePath), null, null)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "MainActivity SaveFile (.png) " + e.message)
|
|
||||||
return false
|
return false
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
out?.close()
|
out?.close()
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
Log.e(TAG, "MainActivity SaveFile (.png) 2 " + e.message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -223,7 +204,6 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
|
||||||
try {
|
try {
|
||||||
Svg.saveSvg(file, my_canvas)
|
Svg.saveSvg(file, my_canvas)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "MainActivity SaveFile (.svg) " + e.message)
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,16 +216,16 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
|
||||||
|
|
||||||
private fun shareImage() {
|
private fun shareImage() {
|
||||||
val shareTitle = resources.getString(R.string.share_via)
|
val shareTitle = resources.getString(R.string.share_via)
|
||||||
val bitmap = my_canvas.bitmap
|
val uri = getImageUri(my_canvas.getBitmap()) ?: return
|
||||||
val sendIntent = Intent()
|
|
||||||
val uri = getImageUri(bitmap) ?: return
|
|
||||||
|
|
||||||
sendIntent.action = Intent.ACTION_SEND
|
Intent().apply {
|
||||||
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
action = Intent.ACTION_SEND
|
||||||
sendIntent.setDataAndType(uri, contentResolver.getType(uri))
|
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||||
sendIntent.putExtra(Intent.EXTRA_STREAM, uri)
|
setDataAndType(uri, contentResolver.getType(uri))
|
||||||
sendIntent.type = "image/*"
|
putExtra(Intent.EXTRA_STREAM, uri)
|
||||||
startActivity(Intent.createChooser(sendIntent, shareTitle))
|
type = "image/*"
|
||||||
|
startActivity(Intent.createChooser(this, shareTitle))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getImageUri(bitmap: Bitmap): Uri? {
|
private fun getImageUri(bitmap: Bitmap): Uri? {
|
||||||
|
@ -264,23 +244,16 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
|
||||||
fileOutputStream = FileOutputStream(file)
|
fileOutputStream = FileOutputStream(file)
|
||||||
fileOutputStream.write(bytes.toByteArray())
|
fileOutputStream.write(bytes.toByteArray())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "getImageUri 1 " + e.message)
|
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
fileOutputStream?.close()
|
fileOutputStream?.close()
|
||||||
} catch (e: IOException) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "getImageUri 2 " + e.message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return FileProvider.getUriForFile(this, "com.simplemobiletools.draw.fileprovider", file)
|
return FileProvider.getUriForFile(this, "com.simplemobiletools.draw.fileprovider", file)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun undo() {
|
|
||||||
my_canvas.undo()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun pickColor() {
|
fun pickColor() {
|
||||||
ColorPickerDialog(this, color) {
|
ColorPickerDialog(this, color) {
|
||||||
setColor(it)
|
setColor(it)
|
||||||
|
|
Loading…
Reference in New Issue