fix #40, add an eraser

This commit is contained in:
tibbi 2017-08-29 22:08:55 +02:00
parent a1891aa229
commit d4dcd21f65
16 changed files with 35 additions and 21 deletions

View File

@ -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) {
if (paintOptions.isEraser)
mPaint.color = mBackgroundColor
else
mPaint.color = paintOptions.color
mPaint.strokeWidth = paintOptions.strokeWidth
}

View File

@ -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)
}

View File

@ -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) {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 531 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1012 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 488 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 483 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

@ -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"/>
<ImageView
android:id="@+id/color_picker"