mirror of
https://github.com/SimpleMobileTools/Simple-Draw.git
synced 2025-02-03 18:27:30 +01:00
Merge commit 'refs/pull/42/head' of github.com:SimpleMobileTools/Simple-Draw
This commit is contained in:
commit
685c69793c
@ -94,6 +94,10 @@ public class MyCanvas extends View {
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
public Map<MyPath,PaintOptions> getPaths() {
|
||||
return mPaths;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
|
@ -2,6 +2,11 @@ package com.simplemobiletools.draw;
|
||||
|
||||
import android.graphics.Path;
|
||||
|
||||
import com.simplemobiletools.draw.actions.Action;
|
||||
import com.simplemobiletools.draw.actions.Line;
|
||||
import com.simplemobiletools.draw.actions.Move;
|
||||
import com.simplemobiletools.draw.actions.Quad;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.Serializable;
|
||||
@ -45,54 +50,7 @@ class MyPath extends Path implements Serializable {
|
||||
super.quadTo(x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
private interface Action extends Serializable {
|
||||
void perform(Path path);
|
||||
}
|
||||
|
||||
private static final class Move implements Action {
|
||||
|
||||
private final float x, y;
|
||||
|
||||
Move(float x, float y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(Path path) {
|
||||
path.moveTo(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Line implements Action {
|
||||
|
||||
private final float x, y;
|
||||
|
||||
Line(float x, float y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(Path path) {
|
||||
path.lineTo(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Quad implements Action {
|
||||
|
||||
private final float x1, y1, x2, y2;
|
||||
|
||||
private Quad(float x1, float y1, float x2, float y2) {
|
||||
this.x1 = x1;
|
||||
this.y1 = y1;
|
||||
this.x2 = x2;
|
||||
this.y2 = y2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(Path path) {
|
||||
path.quadTo(x1, y1, x2, y2);
|
||||
}
|
||||
List<Action> getActions() {
|
||||
return actions;
|
||||
}
|
||||
}
|
||||
|
69
app/src/main/java/com/simplemobiletools/draw/Svg.java
Normal file
69
app/src/main/java/com/simplemobiletools/draw/Svg.java
Normal file
@ -0,0 +1,69 @@
|
||||
package com.simplemobiletools.draw;
|
||||
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
|
||||
import com.simplemobiletools.draw.actions.Action;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Map;
|
||||
|
||||
public class Svg {
|
||||
|
||||
public static void saveSvg(File output, MyCanvas canvas)
|
||||
throws Exception {
|
||||
// This might throw ClassCastException
|
||||
int backgroundColor = ((ColorDrawable) canvas.getBackground()).getColor();
|
||||
|
||||
// This might throw IOException
|
||||
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 color (use a 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("\"/>");
|
||||
|
||||
// Write the paths
|
||||
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\"/>");
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
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;
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.simplemobiletools.draw.actions;
|
||||
|
||||
import android.graphics.Path;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
public final class Line implements Action {
|
||||
|
||||
private final float x, y;
|
||||
|
||||
public Line(float x, float y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(Path path) {
|
||||
path.lineTo(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(Writer writer) throws IOException {
|
||||
writer.write("L");
|
||||
writer.write(String.valueOf(x));
|
||||
writer.write(",");
|
||||
writer.write(String.valueOf(y));
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.simplemobiletools.draw.actions;
|
||||
|
||||
|
||||
import android.graphics.Path;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
public final class Move implements Action {
|
||||
|
||||
private final float x, y;
|
||||
|
||||
public Move(float x, float y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(Path path) {
|
||||
path.moveTo(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(Writer writer) throws IOException {
|
||||
writer.write('M');
|
||||
writer.write(String.valueOf(x));
|
||||
writer.write(',');
|
||||
writer.write(String.valueOf(y));
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.simplemobiletools.draw.actions;
|
||||
|
||||
import android.graphics.Path;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
public final class Quad implements Action {
|
||||
|
||||
private final float x1, y1, x2, y2;
|
||||
|
||||
public Quad(float x1, float y1, float x2, float y2) {
|
||||
this.x1 = x1;
|
||||
this.y1 = y1;
|
||||
this.x2 = x2;
|
||||
this.y2 = y2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(Path path) {
|
||||
path.quadTo(x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(Writer writer) throws IOException {
|
||||
writer.write('Q');
|
||||
writer.write(String.valueOf(x1));
|
||||
writer.write(',');
|
||||
writer.write(String.valueOf(y1));
|
||||
writer.write(' ');
|
||||
writer.write(String.valueOf(x2));
|
||||
writer.write(',');
|
||||
writer.write(String.valueOf(y2));
|
||||
}
|
||||
}
|
@ -20,11 +20,13 @@ import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.SeekBar;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.simplemobiletools.draw.Config;
|
||||
import com.simplemobiletools.draw.MyCanvas;
|
||||
import com.simplemobiletools.draw.R;
|
||||
import com.simplemobiletools.draw.Svg;
|
||||
import com.simplemobiletools.draw.Utils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@ -50,6 +52,7 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange
|
||||
@BindView(R.id.stroke_width_bar) SeekBar mStrokeWidthBar;
|
||||
|
||||
private String curFileName;
|
||||
private int curExtensionIndex;
|
||||
|
||||
private int color;
|
||||
private float strokeWidth;
|
||||
@ -163,6 +166,9 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange
|
||||
|
||||
final EditText fileNameET = (EditText) saveFileView.findViewById(R.id.file_name);
|
||||
fileNameET.setText(curFileName);
|
||||
|
||||
final Spinner fileExtensionS = (Spinner) saveFileView.findViewById(R.id.file_extension);
|
||||
fileExtensionS.setSelection(curExtensionIndex);
|
||||
builder.setView(saveFileView);
|
||||
|
||||
builder.setPositiveButton(R.string.ok, null);
|
||||
@ -174,10 +180,12 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final String fileName = fileNameET.getText().toString().trim();
|
||||
|
||||
final String extension = (String) fileExtensionS.getSelectedItem();
|
||||
if (!fileName.isEmpty()) {
|
||||
if (saveFile(fileName + ".png")) {
|
||||
if (saveFile(fileName, extension)) {
|
||||
curFileName = fileName;
|
||||
curExtensionIndex = fileExtensionS.getSelectedItemPosition();
|
||||
|
||||
Utils.showToast(getApplicationContext(), R.string.saving_ok);
|
||||
alertDialog.dismiss();
|
||||
} else {
|
||||
@ -190,7 +198,7 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange
|
||||
});
|
||||
}
|
||||
|
||||
private boolean saveFile(final String fileName) {
|
||||
private boolean saveFile(final String fileName, final String extension) {
|
||||
final String path = Environment.getExternalStorageDirectory().toString();
|
||||
final File directory = new File(path, SAVE_FOLDER_NAME);
|
||||
if (!directory.exists()) {
|
||||
@ -199,24 +207,41 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange
|
||||
}
|
||||
}
|
||||
|
||||
final Bitmap bitmap = mMyCanvas.getBitmap();
|
||||
FileOutputStream out = null;
|
||||
try {
|
||||
final File file = new File(directory, fileName);
|
||||
out = new FileOutputStream(file);
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
|
||||
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{file.getAbsolutePath()}, null, null);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "MainActivity SaveFile " + e.getMessage());
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
final File file = new File(directory, fileName+extension);
|
||||
switch (extension) {
|
||||
case ".png":
|
||||
final Bitmap bitmap = mMyCanvas.getBitmap();
|
||||
FileOutputStream out = null;
|
||||
try {
|
||||
out = new FileOutputStream(file);
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
|
||||
MediaScannerConnection.scanFile(getApplicationContext(),
|
||||
new String[]{file.getAbsolutePath()}, null, null);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "MainActivity SaveFile (.png) " + e.getMessage());
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "MainActivity SaveFile (.png) 2 " + e.getMessage());
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "MainActivity SaveFile 2 " + e.getMessage());
|
||||
}
|
||||
break;
|
||||
|
||||
case ".svg":
|
||||
try {
|
||||
Svg.saveSvg(file, mMyCanvas);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "MainActivity SaveFile (.svg) " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -20,7 +20,7 @@
|
||||
android:layout_toLeftOf="@+id/file_extension"
|
||||
android:singleLine="true"/>
|
||||
|
||||
<TextView
|
||||
<Spinner
|
||||
android:id="@+id/file_extension"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
@ -29,6 +29,6 @@
|
||||
android:layout_alignTop="@+id/file_name"
|
||||
android:layout_below="@+id/file_name_label"
|
||||
android:gravity="center_vertical"
|
||||
android:text=".png"/>
|
||||
android:entries="@array/save_formats"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
@ -12,6 +12,10 @@
|
||||
<string name="no_permissions">Could not save the file without accessing the external storage</string>
|
||||
<string name="ok">OK</string>
|
||||
<string name="cancel">Cancel</string>
|
||||
<string-array name="save_formats" translatable="false">
|
||||
<item>.png</item>
|
||||
<item>.svg</item>
|
||||
</string-array>
|
||||
|
||||
<!-- Settings -->
|
||||
<string name="settings">Settings</string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user