mirror of
https://github.com/SimpleMobileTools/Simple-Draw.git
synced 2025-06-05 21:59:17 +02:00
correct drawing with different colors
This commit is contained in:
@ -9,12 +9,14 @@ import android.util.AttributeSet;
|
|||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class MyCanvas extends View {
|
public class MyCanvas extends View {
|
||||||
private Paint paint;
|
private Paint paint;
|
||||||
private Path path;
|
private Path path;
|
||||||
private ArrayList<Path> paths;
|
private Map<Path, Integer> paths;
|
||||||
|
private int color;
|
||||||
private float curX;
|
private float curX;
|
||||||
private float curY;
|
private float curY;
|
||||||
private float startX;
|
private float startX;
|
||||||
@ -32,29 +34,37 @@ public class MyCanvas extends View {
|
|||||||
paint.setStrokeWidth(5f);
|
paint.setStrokeWidth(5f);
|
||||||
paint.setAntiAlias(true);
|
paint.setAntiAlias(true);
|
||||||
|
|
||||||
paths = new ArrayList<>();
|
paths = new LinkedHashMap<>();
|
||||||
paths.add(path);
|
paths.put(path, paint.getColor());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undo() {
|
public void undo() {
|
||||||
if (paths.size() <= 0)
|
if (paths.size() <= 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
paths.remove(paths.size() - 1);
|
Path lastKey = null;
|
||||||
|
for (Path key : paths.keySet()) {
|
||||||
|
lastKey = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
paths.remove(lastKey);
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setColor(int color) {
|
public void setColor(int newColor) {
|
||||||
paint.setColor(color);
|
color = newColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDraw(Canvas canvas) {
|
protected void onDraw(Canvas canvas) {
|
||||||
super.onDraw(canvas);
|
super.onDraw(canvas);
|
||||||
|
|
||||||
for (Path p : paths) {
|
for (Map.Entry<Path, Integer> entry : paths.entrySet()) {
|
||||||
canvas.drawPath(p, paint);
|
paint.setColor(entry.getValue());
|
||||||
|
canvas.drawPath(entry.getKey(), paint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
paint.setColor(color);
|
||||||
canvas.drawPath(path, paint);
|
canvas.drawPath(path, paint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +91,7 @@ public class MyCanvas extends View {
|
|||||||
path.lineTo(curX + 1, curY);
|
path.lineTo(curX + 1, curY);
|
||||||
}
|
}
|
||||||
|
|
||||||
paths.add(path);
|
paths.put(path, paint.getColor());
|
||||||
path = new Path();
|
path = new Path();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,7 +117,6 @@ public class MyCanvas extends View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
invalidate();
|
invalidate();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user