converting a couple more classes to kotlin
This commit is contained in:
parent
9abc45b9a8
commit
96fa329ea5
|
@ -35,11 +35,11 @@ public class MyCanvas extends View {
|
|||
mPath = new MyPath();
|
||||
mPaint = new Paint();
|
||||
mPaintOptions = new PaintOptions();
|
||||
mPaint.setColor(mPaintOptions.color);
|
||||
mPaint.setColor(mPaintOptions.getColor());
|
||||
mPaint.setStyle(Paint.Style.STROKE);
|
||||
mPaint.setStrokeJoin(Paint.Join.ROUND);
|
||||
mPaint.setStrokeCap(Paint.Cap.ROUND);
|
||||
mPaint.setStrokeWidth(mPaintOptions.strokeWidth);
|
||||
mPaint.setStrokeWidth(mPaintOptions.getStrokeWidth());
|
||||
mPaint.setAntiAlias(true);
|
||||
|
||||
mPaths = new LinkedHashMap<>();
|
||||
|
@ -66,14 +66,14 @@ public class MyCanvas extends View {
|
|||
}
|
||||
|
||||
public void setColor(int newColor) {
|
||||
mPaintOptions.color = newColor;
|
||||
mPaintOptions.setColor(newColor);
|
||||
if (mIsStrokeWidthBarEnabled) {
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
public void setStrokeWidth(float newStrokeWidth) {
|
||||
mPaintOptions.strokeWidth = newStrokeWidth;
|
||||
mPaintOptions.setStrokeWidth(newStrokeWidth);
|
||||
if (mIsStrokeWidthBarEnabled) {
|
||||
invalidate();
|
||||
}
|
||||
|
@ -125,20 +125,20 @@ public class MyCanvas extends View {
|
|||
mPaint.setStyle(Paint.Style.FILL);
|
||||
|
||||
float y = getHeight() - res.getDimension(R.dimen.preview_dot_offset_y);
|
||||
canvas.drawCircle(getWidth() / 2, y, mPaintOptions.strokeWidth / 2, mPaint);
|
||||
canvas.drawCircle(getWidth() / 2, y, mPaintOptions.getStrokeWidth() / 2, mPaint);
|
||||
mPaint.setStyle(Paint.Style.STROKE);
|
||||
mPaint.setColor(Utils.shouldUseWhite(mPaintOptions.color) ? Color.WHITE : Color.BLACK);
|
||||
mPaint.setColor(Utils.shouldUseWhite(mPaintOptions.getColor()) ? Color.WHITE : Color.BLACK);
|
||||
mPaint.setStrokeWidth(res.getDimension(R.dimen.preview_dot_stroke_size));
|
||||
|
||||
y = getHeight() - res.getDimension(R.dimen.preview_dot_offset_y);
|
||||
float radius = (mPaintOptions.strokeWidth + res.getDimension(R.dimen.preview_dot_stroke_size)) / 2;
|
||||
float radius = (mPaintOptions.getStrokeWidth() + res.getDimension(R.dimen.preview_dot_stroke_size)) / 2;
|
||||
canvas.drawCircle(getWidth() / 2, y, radius, mPaint);
|
||||
changePaint(mPaintOptions);
|
||||
}
|
||||
|
||||
private void changePaint(PaintOptions paintOptions) {
|
||||
mPaint.setColor(paintOptions.color);
|
||||
mPaint.setStrokeWidth(paintOptions.strokeWidth);
|
||||
mPaint.setColor(paintOptions.getColor());
|
||||
mPaint.setStrokeWidth(paintOptions.getStrokeWidth());
|
||||
}
|
||||
|
||||
public void clearCanvas() {
|
||||
|
@ -174,7 +174,7 @@ public class MyCanvas extends View {
|
|||
mPaths.put(mPath, mPaintOptions);
|
||||
pathsUpdated();
|
||||
mPath = new MyPath();
|
||||
mPaintOptions = new PaintOptions(mPaintOptions.color, mPaintOptions.strokeWidth);
|
||||
mPaintOptions = new PaintOptions(mPaintOptions.getColor(), mPaintOptions.getStrokeWidth());
|
||||
}
|
||||
|
||||
private void pathsUpdated() {
|
||||
|
@ -248,8 +248,8 @@ public class MyCanvas extends View {
|
|||
for (Map.Entry<MyPath, PaintOptions> entry : mPaths.entrySet()) {
|
||||
out.writeSerializable(entry.getKey());
|
||||
PaintOptions paintOptions = entry.getValue();
|
||||
out.writeInt(paintOptions.color);
|
||||
out.writeFloat(paintOptions.strokeWidth);
|
||||
out.writeInt(paintOptions.getColor());
|
||||
out.writeFloat(paintOptions.getStrokeWidth());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
package com.simplemobiletools.draw;
|
||||
|
||||
import android.graphics.Color;
|
||||
|
||||
class PaintOptions {
|
||||
int color = Color.BLACK;
|
||||
float strokeWidth = 5f;
|
||||
|
||||
PaintOptions() {
|
||||
|
||||
}
|
||||
|
||||
PaintOptions(int color, float strokeWidth) {
|
||||
this.color = color;
|
||||
this.strokeWidth = strokeWidth;
|
||||
}
|
||||
}
|
|
@ -1,191 +0,0 @@
|
|||
package com.simplemobiletools.draw;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.sax.Element;
|
||||
import android.sax.RootElement;
|
||||
import android.sax.StartElementListener;
|
||||
import android.util.Xml;
|
||||
|
||||
import com.simplemobiletools.draw.actions.Action;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Serializable;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
public class Svg {
|
||||
|
||||
//region Saving
|
||||
|
||||
public static void saveSvg(File output, MyCanvas canvas) throws Exception {
|
||||
int backgroundColor = ((ColorDrawable) canvas.getBackground()).getColor();
|
||||
|
||||
FileOutputStream out = new FileOutputStream(output);
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
|
||||
writeSvg(writer, backgroundColor, canvas.getPaths(), canvas.getWidth(), canvas.getHeight());
|
||||
writer.close();
|
||||
}
|
||||
|
||||
private static void writeSvg(Writer writer, int backgroundColor, Map<MyPath, PaintOptions> paths, int width, int height) throws IOException {
|
||||
writer.write("<svg width=\"");
|
||||
writer.write(String.valueOf(width));
|
||||
writer.write("\" height=\"");
|
||||
writer.write(String.valueOf(height));
|
||||
writer.write("\" xmlns=\"http://www.w3.org/2000/svg\">");
|
||||
|
||||
// background rect
|
||||
writer.write("<rect width=\"");
|
||||
writer.write(String.valueOf(width));
|
||||
writer.write("\" height=\"");
|
||||
writer.write(String.valueOf(height));
|
||||
writer.write("\" fill=\"#");
|
||||
writer.write(Integer.toHexString(backgroundColor).substring(2)); // Skip the alpha FF
|
||||
writer.write("\"/>");
|
||||
|
||||
for (Map.Entry<MyPath, PaintOptions> entry : paths.entrySet()) {
|
||||
writePath(writer, entry.getKey(), entry.getValue());
|
||||
}
|
||||
writer.write("</svg>");
|
||||
}
|
||||
|
||||
private static void writePath(Writer writer, MyPath path, PaintOptions options) throws IOException {
|
||||
writer.write("<path d=\"");
|
||||
for (Action action : path.getActions()) {
|
||||
action.perform(writer);
|
||||
writer.write(' ');
|
||||
}
|
||||
|
||||
writer.write("\" fill=\"none\" stroke=\"#");
|
||||
writer.write(Integer.toHexString(options.color).substring(2)); // Skip the alpha FF
|
||||
writer.write("\" stroke-width=\"");
|
||||
writer.write(String.valueOf(options.strokeWidth));
|
||||
writer.write("\" stroke-linecap=\"round\"/>");
|
||||
}
|
||||
|
||||
//endregion
|
||||
|
||||
//region Loading
|
||||
|
||||
public static void loadSvg(File file, MyCanvas canvas) throws Exception {
|
||||
SSvg svg = parseSvg(file);
|
||||
|
||||
canvas.clearCanvas();
|
||||
canvas.setBackgroundColor(svg.background.color);
|
||||
|
||||
for (SPath sp : svg.paths) {
|
||||
MyPath path = new MyPath();
|
||||
path.readObject(sp.data);
|
||||
PaintOptions options = new PaintOptions(sp.color, sp.strokeWidth);
|
||||
|
||||
canvas.addPath(path, options);
|
||||
}
|
||||
}
|
||||
|
||||
public static SSvg parseSvg(File file) throws Exception {
|
||||
InputStream is = null;
|
||||
final SSvg svg = new SSvg();
|
||||
try {
|
||||
is = new FileInputStream(file);
|
||||
|
||||
// Actual parsing (http://stackoverflow.com/a/4828765)
|
||||
final String ns = "http://www.w3.org/2000/svg";
|
||||
RootElement root = new RootElement(ns, "svg");
|
||||
Element rectElement = root.getChild(ns, "rect");
|
||||
final Element pathElement = root.getChild(ns, "path");
|
||||
|
||||
root.setStartElementListener(new StartElementListener() {
|
||||
@Override
|
||||
public void start(Attributes attributes) {
|
||||
int width = Integer.parseInt(attributes.getValue("width"));
|
||||
int height = Integer.parseInt(attributes.getValue("height"));
|
||||
svg.setSize(width, height);
|
||||
}
|
||||
});
|
||||
|
||||
rectElement.setStartElementListener(new StartElementListener() {
|
||||
@Override
|
||||
public void start(Attributes attributes) {
|
||||
int width = Integer.parseInt(attributes.getValue("width"));
|
||||
int height = Integer.parseInt(attributes.getValue("height"));
|
||||
int color = Color.parseColor(attributes.getValue("fill"));
|
||||
if (svg.background != null)
|
||||
throw new UnsupportedOperationException("Unsupported SVG, should only have one <rect>.");
|
||||
|
||||
svg.background = new SRect(width, height, color);
|
||||
}
|
||||
});
|
||||
|
||||
pathElement.setStartElementListener(new StartElementListener() {
|
||||
public void start(Attributes attributes) {
|
||||
String d = attributes.getValue("d");
|
||||
int color = Color.parseColor(attributes.getValue("stroke"));
|
||||
float width = Float.parseFloat(attributes.getValue("stroke-width"));
|
||||
svg.paths.add(new SPath(d, color, width));
|
||||
}
|
||||
});
|
||||
|
||||
// Once the parsing is set up, parse this InputStream
|
||||
Xml.parse(is, Xml.Encoding.UTF_8, root.getContentHandler());
|
||||
} finally {
|
||||
if (is != null)
|
||||
is.close();
|
||||
}
|
||||
return svg;
|
||||
}
|
||||
|
||||
//region Svg serializable classes
|
||||
|
||||
private static class SSvg implements Serializable {
|
||||
int width;
|
||||
int height;
|
||||
SRect background;
|
||||
final ArrayList<SPath> paths;
|
||||
|
||||
SSvg() {
|
||||
paths = new ArrayList<>();
|
||||
}
|
||||
|
||||
void setSize(int w, int h) {
|
||||
width = w;
|
||||
height = h;
|
||||
}
|
||||
}
|
||||
|
||||
private static class SRect implements Serializable {
|
||||
final int width;
|
||||
final int height;
|
||||
final int color;
|
||||
|
||||
SRect(int w, int h, int c) {
|
||||
width = w;
|
||||
height = h;
|
||||
color = c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class SPath implements Serializable {
|
||||
String data;
|
||||
int color;
|
||||
float strokeWidth;
|
||||
|
||||
SPath(String d, int c, float w) {
|
||||
data = d;
|
||||
color = c;
|
||||
strokeWidth = w;
|
||||
}
|
||||
}
|
||||
|
||||
//endregion
|
||||
|
||||
//endregion
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
package com.simplemobiletools.draw.actions;
|
||||
|
||||
import android.graphics.Path;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.io.Writer;
|
||||
|
||||
public interface Action extends Serializable {
|
||||
void perform(Path path);
|
||||
|
||||
void perform(Writer writer) throws IOException;
|
||||
}
|
|
@ -243,7 +243,7 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange
|
|||
}
|
||||
case ".svg": {
|
||||
try {
|
||||
Svg.saveSvg(file, mMyCanvas);
|
||||
Svg.INSTANCE.saveSvg(file, mMyCanvas);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "MainActivity SaveFile (.svg) " + e.getMessage());
|
||||
return false;
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package com.simplemobiletools.draw
|
||||
|
||||
import android.graphics.Color
|
||||
|
||||
internal class PaintOptions {
|
||||
var color = Color.BLACK
|
||||
var strokeWidth = 5f
|
||||
|
||||
constructor()
|
||||
|
||||
constructor(color: Int, strokeWidth: Float) {
|
||||
this.color = color
|
||||
this.strokeWidth = strokeWidth
|
||||
}
|
||||
}
|
|
@ -0,0 +1,133 @@
|
|||
package com.simplemobiletools.draw
|
||||
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.sax.RootElement
|
||||
import android.util.Xml
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
|
||||
object Svg {
|
||||
@Throws(Exception::class)
|
||||
fun saveSvg(output: File, canvas: MyCanvas) {
|
||||
val backgroundColor = (canvas.background as ColorDrawable).color
|
||||
|
||||
val out = FileOutputStream(output)
|
||||
val writer = BufferedWriter(OutputStreamWriter(out))
|
||||
writeSvg(writer, backgroundColor, canvas.paths, canvas.width, canvas.height)
|
||||
writer.close()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun writeSvg(writer: Writer, backgroundColor: Int, paths: Map<MyPath, PaintOptions>, width: Int, height: Int) {
|
||||
writer.write("<svg width=\"")
|
||||
writer.write(width.toString())
|
||||
writer.write("\" height=\"")
|
||||
writer.write(height.toString())
|
||||
writer.write("\" xmlns=\"http://www.w3.org/2000/svg\">")
|
||||
|
||||
// background rect
|
||||
writer.write("<rect width=\"")
|
||||
writer.write(width.toString())
|
||||
writer.write("\" height=\"")
|
||||
writer.write(height.toString())
|
||||
writer.write("\" fill=\"#")
|
||||
writer.write(Integer.toHexString(backgroundColor).substring(2)) // Skip the alpha FF
|
||||
writer.write("\"/>")
|
||||
|
||||
for ((key, value) in paths) {
|
||||
writePath(writer, key, value)
|
||||
}
|
||||
writer.write("</svg>")
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun writePath(writer: Writer, path: MyPath, options: PaintOptions) {
|
||||
writer.write("<path d=\"")
|
||||
for (action in path.getActions()) {
|
||||
action.perform(writer)
|
||||
writer.write(" ")
|
||||
}
|
||||
|
||||
writer.write("\" fill=\"none\" stroke=\"#")
|
||||
writer.write(Integer.toHexString(options.color).substring(2)) // Skip the alpha FF
|
||||
writer.write("\" stroke-width=\"")
|
||||
writer.write(options.strokeWidth.toString())
|
||||
writer.write("\" stroke-linecap=\"round\"/>")
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun loadSvg(file: File, canvas: MyCanvas) {
|
||||
val svg = parseSvg(file)
|
||||
|
||||
canvas.clearCanvas()
|
||||
canvas.setBackgroundColor(svg.background!!.color)
|
||||
|
||||
for (sp in svg.paths) {
|
||||
val path = MyPath()
|
||||
path.readObject(sp.data)
|
||||
val options = PaintOptions(sp.color, sp.strokeWidth)
|
||||
|
||||
canvas.addPath(path, options)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
private fun parseSvg(file: File): SSvg {
|
||||
var inputStream: InputStream? = null
|
||||
val svg = SSvg()
|
||||
try {
|
||||
inputStream = FileInputStream(file)
|
||||
|
||||
// Actual parsing (http://stackoverflow.com/a/4828765)
|
||||
val ns = "http://www.w3.org/2000/svg"
|
||||
val root = RootElement(ns, "svg")
|
||||
val rectElement = root.getChild(ns, "rect")
|
||||
val pathElement = root.getChild(ns, "path")
|
||||
|
||||
root.setStartElementListener { attributes ->
|
||||
val width = Integer.parseInt(attributes.getValue("width"))
|
||||
val height = Integer.parseInt(attributes.getValue("height"))
|
||||
svg.setSize(width, height)
|
||||
}
|
||||
|
||||
rectElement.setStartElementListener { attributes ->
|
||||
val width = Integer.parseInt(attributes.getValue("width"))
|
||||
val height = Integer.parseInt(attributes.getValue("height"))
|
||||
val color = Color.parseColor(attributes.getValue("fill"))
|
||||
if (svg.background != null)
|
||||
throw UnsupportedOperationException("Unsupported SVG, should only have one <rect>.")
|
||||
|
||||
svg.background = SRect(width, height, color)
|
||||
}
|
||||
|
||||
pathElement.setStartElementListener { attributes ->
|
||||
val d = attributes.getValue("d")
|
||||
val color = Color.parseColor(attributes.getValue("stroke"))
|
||||
val width = java.lang.Float.parseFloat(attributes.getValue("stroke-width"))
|
||||
svg.paths.add(SPath(d, color, width))
|
||||
}
|
||||
|
||||
Xml.parse(inputStream, Xml.Encoding.UTF_8, root.contentHandler)
|
||||
} finally {
|
||||
inputStream?.close()
|
||||
}
|
||||
return svg
|
||||
}
|
||||
|
||||
private class SSvg internal constructor() : Serializable {
|
||||
internal var width: Int = 0
|
||||
internal var height: Int = 0
|
||||
internal var background: SRect? = null
|
||||
internal val paths: ArrayList<SPath> = ArrayList()
|
||||
|
||||
internal fun setSize(w: Int, h: Int) {
|
||||
width = w
|
||||
height = h
|
||||
}
|
||||
}
|
||||
|
||||
private class SRect internal constructor(internal val width: Int, internal val height: Int, internal val color: Int) : Serializable
|
||||
|
||||
private class SPath internal constructor(internal var data: String, internal var color: Int, internal var strokeWidth: Float) : Serializable
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.simplemobiletools.draw.actions
|
||||
|
||||
import android.graphics.Path
|
||||
|
||||
import java.io.IOException
|
||||
import java.io.Serializable
|
||||
import java.io.Writer
|
||||
|
||||
interface Action : Serializable {
|
||||
fun perform(path: Path)
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun perform(writer: Writer)
|
||||
}
|
Loading…
Reference in New Issue