diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/MyCanvas.kt b/app/src/main/kotlin/com/simplemobiletools/draw/MyCanvas.kt index 250b167..5dcb4df 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/MyCanvas.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/MyCanvas.kt @@ -30,6 +30,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { private var mStartY = 0f private var mIsSaving = false private var mIsStrokeWidthBarEnabled = false + private var mIsEraserOn = false + private var mBackgroundColor = 0 init { mPaint.apply { @@ -59,8 +61,10 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { invalidate() } - fun toggleEraser() { - + fun toggleEraser(isEraserOn: Boolean) { + mIsEraserOn = isEraserOn + mPaintOptions.isEraser = isEraserOn + invalidate() } fun setColor(newColor: Int) { @@ -70,6 +74,12 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { } } + fun updateBackgroundColor(newColor: Int) { + mBackgroundColor = newColor + setBackgroundColor(newColor) + mBackgroundBitmap = null + } + fun setStrokeWidth(newStrokeWidth: Float) { mPaintOptions.strokeWidth = newStrokeWidth if (mIsStrokeWidthBarEnabled) { @@ -152,7 +162,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { var y = height - res.getDimension(R.dimen.preview_dot_offset_y) canvas.drawCircle((width / 2).toFloat(), y, mPaintOptions.strokeWidth / 2, mPaint) mPaint.style = Paint.Style.STROKE - mPaint.color = mPaintOptions.color.getContrastColor() + mPaint.color = if (mPaintOptions.isEraser) mBackgroundColor.getContrastColor() else mPaintOptions.color.getContrastColor() mPaint.strokeWidth = res.getDimension(R.dimen.preview_dot_stroke_size) y = height - res.getDimension(R.dimen.preview_dot_offset_y) @@ -162,7 +172,11 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { } private fun changePaint(paintOptions: PaintOptions) { - mPaint.color = paintOptions.color + if (paintOptions.isEraser) + mPaint.color = mBackgroundColor + else + mPaint.color = paintOptions.color + mPaint.strokeWidth = paintOptions.strokeWidth } diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/PaintOptions.kt b/app/src/main/kotlin/com/simplemobiletools/draw/PaintOptions.kt index 96aede7..e29002e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/PaintOptions.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/PaintOptions.kt @@ -2,18 +2,6 @@ package com.simplemobiletools.draw import android.graphics.Color -class PaintOptions { - var color = Color.BLACK - var strokeWidth = 5f - var isEraser = false - - constructor() - - constructor(color: Int, strokeWidth: Float, isEraser: Boolean) { - this.color = color - this.strokeWidth = strokeWidth - this.isEraser = isEraser - } - +data class PaintOptions(var color: Int = Color.BLACK, var strokeWidth: Float = 5f, var isEraser: Boolean = false) { fun getColorToExport() = if (isEraser) "none" else Integer.toHexString(color).substring(2) } diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt index 1b45ef0..81879d5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt @@ -44,6 +44,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener { private var strokeWidth = 0f private var suggestedFileExtension = PNG private var openFileIntentPath = "" + private var isEraserOn = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -60,7 +61,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener { color_picker.setOnClickListener { pickColor() } undo.setOnClickListener { my_canvas.undo() } - eraser.setOnClickListener { my_canvas.toggleEraser() } + eraser.setOnClickListener { eraserClicked() } storeStoragePaths() if (intent?.action == Intent.ACTION_VIEW && intent.data != null) { @@ -159,6 +160,16 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener { } } + private fun eraserClicked() { + isEraserOn = !isEraserOn + updateEraserState() + } + + private fun updateEraserState() { + eraser.setImageDrawable(resources.getDrawable(if (isEraserOn) R.drawable.ic_eraser_on else R.drawable.ic_eraser_off)) + my_canvas.toggleEraser(isEraserOn) + } + private fun changeBackgroundClicked() { val oldColor = (my_canvas.background as ColorDrawable).color ColorPickerDialog(this, oldColor) { @@ -232,8 +243,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener { fun setBackgroundColor(pickedColor: Int) { undo.setColorFilter(pickedColor.getContrastColor(), PorterDuff.Mode.SRC_IN) eraser.setColorFilter(pickedColor.getContrastColor(), PorterDuff.Mode.SRC_IN) - my_canvas.setBackgroundColor(pickedColor) - my_canvas.mBackgroundBitmap = null + my_canvas.updateBackgroundColor(pickedColor) suggestedFileExtension = PNG } @@ -241,6 +251,8 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener { color = pickedColor color_picker.setBackgroundColor(color) my_canvas.setColor(color) + isEraserOn = false + updateEraserState() } override fun pathsChanged(cnt: Int) { diff --git a/app/src/main/res/drawable-hdpi/ic_eraser.png b/app/src/main/res/drawable-hdpi/ic_eraser.png deleted file mode 100644 index 5849413..0000000 Binary files a/app/src/main/res/drawable-hdpi/ic_eraser.png and /dev/null differ diff --git a/app/src/main/res/drawable-hdpi/ic_eraser_off.png b/app/src/main/res/drawable-hdpi/ic_eraser_off.png new file mode 100644 index 0000000..2f6292b Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_eraser_off.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_eraser_on.png b/app/src/main/res/drawable-hdpi/ic_eraser_on.png new file mode 100644 index 0000000..3b1c5a9 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_eraser_on.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_eraser.png b/app/src/main/res/drawable-xhdpi/ic_eraser.png deleted file mode 100644 index 035c01d..0000000 Binary files a/app/src/main/res/drawable-xhdpi/ic_eraser.png and /dev/null differ diff --git a/app/src/main/res/drawable-xhdpi/ic_eraser_off.png b/app/src/main/res/drawable-xhdpi/ic_eraser_off.png new file mode 100644 index 0000000..36990ac Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_eraser_off.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_eraser_on.png b/app/src/main/res/drawable-xhdpi/ic_eraser_on.png new file mode 100644 index 0000000..6df0567 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_eraser_on.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_eraser.png b/app/src/main/res/drawable-xxhdpi/ic_eraser.png deleted file mode 100644 index 8909a9c..0000000 Binary files a/app/src/main/res/drawable-xxhdpi/ic_eraser.png and /dev/null differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_eraser_off.png b/app/src/main/res/drawable-xxhdpi/ic_eraser_off.png new file mode 100644 index 0000000..8e1d09b Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_eraser_off.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_eraser_on.png b/app/src/main/res/drawable-xxhdpi/ic_eraser_on.png new file mode 100644 index 0000000..5d68e1d Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_eraser_on.png differ diff --git a/app/src/main/res/drawable-xxxhdpi/ic_eraser.png b/app/src/main/res/drawable-xxxhdpi/ic_eraser.png deleted file mode 100644 index 3f83e03..0000000 Binary files a/app/src/main/res/drawable-xxxhdpi/ic_eraser.png and /dev/null differ diff --git a/app/src/main/res/drawable-xxxhdpi/ic_eraser_off.png b/app/src/main/res/drawable-xxxhdpi/ic_eraser_off.png new file mode 100644 index 0000000..0106a6a Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/ic_eraser_off.png differ diff --git a/app/src/main/res/drawable-xxxhdpi/ic_eraser_on.png b/app/src/main/res/drawable-xxxhdpi/ic_eraser_on.png new file mode 100644 index 0000000..346c8df Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/ic_eraser_on.png differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index d21f7fe..1bac9a9 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -17,7 +17,7 @@ android:layout_height="@dimen/normal_icon_size" android:layout_toLeftOf="@+id/color_picker" android:padding="@dimen/normal_margin" - android:src="@drawable/ic_eraser"/> + android:src="@drawable/ic_eraser_off"/>