properly handle exporting and importing eraser, by setting "none" stroke

This commit is contained in:
tibbi 2017-08-29 16:45:21 +02:00
parent 768b7ac6ab
commit a1891aa229
3 changed files with 15 additions and 8 deletions

View file

@ -200,7 +200,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
mPaths.put(mPath, mPaintOptions)
pathsUpdated()
mPath = MyPath()
mPaintOptions = PaintOptions(mPaintOptions.color, mPaintOptions.strokeWidth)
mPaintOptions = PaintOptions(mPaintOptions.color, mPaintOptions.strokeWidth, mPaintOptions.isEraser)
}
private fun pathsUpdated() {
@ -256,7 +256,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
val size = parcel.readInt()
for (i in 0 until size) {
val key = parcel.readSerializable() as MyPath
val paintOptions = PaintOptions(parcel.readInt(), parcel.readFloat())
val paintOptions = PaintOptions(parcel.readInt(), parcel.readFloat(), parcel.readInt() == 1)
paths.put(key, paintOptions)
}
}
@ -268,6 +268,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
out.writeSerializable(path)
out.writeInt(paintOptions.color)
out.writeFloat(paintOptions.strokeWidth)
out.writeInt(if (paintOptions.isEraser) 1 else 0)
}
}

View file

@ -5,11 +5,15 @@ import android.graphics.Color
class PaintOptions {
var color = Color.BLACK
var strokeWidth = 5f
var isEraser = false
constructor()
constructor(color: Int, strokeWidth: Float) {
constructor(color: Int, strokeWidth: Float, isEraser: Boolean) {
this.color = color
this.strokeWidth = strokeWidth
this.isEraser = isEraser
}
fun getColorToExport() = if (isEraser) "none" else Integer.toHexString(color).substring(2)
}

View file

@ -44,7 +44,7 @@ object Svg {
}
write("\" fill=\"none\" stroke=\"#")
write(Integer.toHexString(options.color).substring(2))
write(options.getColorToExport())
write("\" stroke-width=\"")
write(options.strokeWidth.toString())
write("\" stroke-linecap=\"round\"/>")
@ -60,7 +60,7 @@ object Svg {
svg.paths.forEach {
val path = MyPath()
path.readObject(it.data, activity)
val options = PaintOptions(it.color, it.strokeWidth)
val options = PaintOptions(it.color, it.strokeWidth, it.isEraser)
canvas.addPath(path, options)
}
@ -96,9 +96,11 @@ object Svg {
pathElement.setStartElementListener { attributes ->
val d = attributes.getValue("d")
val color = Color.parseColor(attributes.getValue("stroke"))
val width = attributes.getValue("stroke-width").toFloat()
svg.paths.add(SPath(d, color, width))
val stroke = attributes.getValue("stroke")
val isEraser = stroke == "none"
val color = if (isEraser) 0 else Color.parseColor(stroke)
svg.paths.add(SPath(d, color, width, isEraser))
}
Xml.parse(inputStream, Xml.Encoding.UTF_8, root.contentHandler)
@ -122,5 +124,5 @@ object Svg {
private class SRect(val width: Int, val height: Int, val color: Int) : Serializable
private class SPath(var data: String, var color: Int, var strokeWidth: Float) : Serializable
private class SPath(var data: String, var color: Int, var strokeWidth: Float, var isEraser: Boolean) : Serializable
}