Add .svg loading logic
This commit is contained in:
parent
e4ba503c70
commit
d4c041d06e
|
@ -98,6 +98,11 @@ public class MyCanvas extends View {
|
|||
return mPaths;
|
||||
}
|
||||
|
||||
public void addPath(MyPath path, PaintOptions options) {
|
||||
mPaths.put(path, options);
|
||||
pathsUpdated();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
|
|
|
@ -10,6 +10,7 @@ import com.simplemobiletools.draw.actions.Quad;
|
|||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.Serializable;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -26,12 +27,47 @@ class MyPath extends Path implements Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
public void readObject(String pathData) throws InvalidParameterException {
|
||||
String[] tokens = pathData.split("\\s+");
|
||||
for (int i = 0; i < tokens.length; ++i) {
|
||||
switch (tokens[i].charAt(0)) {
|
||||
case 'M':
|
||||
addAction(new Move(tokens[i]));
|
||||
break;
|
||||
case 'L':
|
||||
addAction(new Line(tokens[i]));
|
||||
break;
|
||||
case 'Q':
|
||||
// Quad actions are of the following form:
|
||||
// "Qx1,y1 x2,y2"
|
||||
// Since we split the tokens by whitespace, we need to join them again
|
||||
if (i+1 >= tokens.length)
|
||||
throw new InvalidParameterException("Error parsing the data for a Quad.");
|
||||
|
||||
addAction(new Quad(tokens[i]+" "+tokens[i+1]));
|
||||
++i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
actions.clear();
|
||||
super.reset();
|
||||
}
|
||||
|
||||
private void addAction(Action action) {
|
||||
if (action instanceof Move) {
|
||||
moveTo(((Move)action).x, ((Move)action).y);
|
||||
} else if (action instanceof Line) {
|
||||
lineTo(((Line)action).x, ((Line)action).y);
|
||||
} else if (action instanceof Quad) {
|
||||
final Quad q = (Quad)action;
|
||||
quadTo(q.x1, q.y1, q.x2, q.y2);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveTo(float x, float y) {
|
||||
actions.add(new Move(x, y));
|
||||
|
|
|
@ -76,7 +76,22 @@ public class Svg {
|
|||
|
||||
//region Loading
|
||||
|
||||
public static void parseSvg(File file) throws Exception {
|
||||
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 {
|
||||
|
@ -125,6 +140,7 @@ public class Svg {
|
|||
if (is != null)
|
||||
is.close();
|
||||
}
|
||||
return svg;
|
||||
}
|
||||
|
||||
//region Svg serializable classes
|
||||
|
|
|
@ -4,10 +4,24 @@ import android.graphics.Path;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.security.InvalidParameterException;
|
||||
|
||||
public final class Line implements Action {
|
||||
|
||||
private final float x, y;
|
||||
public final float x, y;
|
||||
|
||||
public Line(String data) {
|
||||
if (data.startsWith("L"))
|
||||
throw new InvalidParameterException("The Line data should start with 'L'.");
|
||||
|
||||
try {
|
||||
String[] xy = data.substring(1).split(",");
|
||||
x = Float.parseFloat(xy[0].trim());
|
||||
y = Float.parseFloat(xy[1].trim());
|
||||
} catch (Exception ignored) {
|
||||
throw new InvalidParameterException("Error parsing the given Line data.");
|
||||
}
|
||||
}
|
||||
|
||||
public Line(float x, float y) {
|
||||
this.x = x;
|
||||
|
|
|
@ -4,10 +4,24 @@ import android.graphics.Path;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.security.InvalidParameterException;
|
||||
|
||||
public final class Move implements Action {
|
||||
|
||||
private final float x, y;
|
||||
public final float x, y;
|
||||
|
||||
public Move(String data) throws InvalidParameterException {
|
||||
if (data.startsWith("M"))
|
||||
throw new InvalidParameterException("The Move data should start with 'M'.");
|
||||
|
||||
try {
|
||||
String[] xy = data.substring(1).split(",");
|
||||
x = Float.parseFloat(xy[0].trim());
|
||||
y = Float.parseFloat(xy[1].trim());
|
||||
} catch (Exception ignored) {
|
||||
throw new InvalidParameterException("Error parsing the given Move data.");
|
||||
}
|
||||
}
|
||||
|
||||
public Move(float x, float y) {
|
||||
this.x = x;
|
||||
|
|
|
@ -4,10 +4,29 @@ import android.graphics.Path;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.security.InvalidParameterException;
|
||||
|
||||
public final class Quad implements Action {
|
||||
|
||||
private final float x1, y1, x2, y2;
|
||||
public final float x1, y1, x2, y2;
|
||||
|
||||
public Quad(String data) {
|
||||
if (data.startsWith("Q"))
|
||||
throw new InvalidParameterException("The Quad data should start with 'Q'.");
|
||||
|
||||
try {
|
||||
String[] parts = data.split("\\s+");
|
||||
String[] xy1 = parts[0].substring(1).split(",");
|
||||
String[] xy2 = parts[1].split(","); // No need to skip the 'Q' here
|
||||
|
||||
x1 = Float.parseFloat(xy1[0].trim());
|
||||
y1 = Float.parseFloat(xy1[1].trim());
|
||||
x2 = Float.parseFloat(xy2[0].trim());
|
||||
y2 = Float.parseFloat(xy2[1].trim());
|
||||
} catch (Exception ignored) {
|
||||
throw new InvalidParameterException("Error parsing the given Quad data.");
|
||||
}
|
||||
}
|
||||
|
||||
public Quad(float x1, float y1, float x2, float y2) {
|
||||
this.x1 = x1;
|
||||
|
|
Loading…
Reference in New Issue