I am making a drawing app and it has layers. Each time I draw a line it disappears. The process is like this :
- Draw a line with path and add it to path ArrayList once TOUCH_UP happens (I lift my finger)
- invalidate is called and then canvas draws all paths stored in the arraylist
Instead of such behavior my path disappears the moment it is drawn and canvas is blank. Methods :
(path,layer1,layer2,layer3 are all ArrayList of Path type)
// constructor where path arraylist is pointing to currently used layer arraylist (all contain path types)
public ArtView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
setupDrawing();
layer = 1;
layer1 = new ArrayList<>();
layer2 = new ArrayList<>();
layer3 = new ArrayList<>();
path = layer1;
undo = new ArrayList<>();
}
// onDraw is called each time to update canvas with each change
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
// load a picture from galery if under is true, make a new bitmap if false
if (loadedBitmap != null) {
canvas.drawBitmap(loadedBitmap, 0, 0, paintLine);
} else {
canvas.drawBitmap(bitmap, 0, 0, canvasPaint);
}
// for each layer arraylist draw all their paths on canvas
for (Path path : layer1) {
canvas.drawPath(path, paintLine);
}
for (Path path : layer2) {
canvas.drawPath(path, paintLine);
}
for (Path path : layer3) {
canvas.drawPath(path, paintLine);
}
canvas.restore();
}
// touch method that handles drawing
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
mPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
path.add(mPath); // add currently created path to arraylist to be painted in onDraw
mPath.reset();
break;
default:
return false;
}
invalidate(); // call onDraw and update changes
return true;
}
// set up method called when starting this activity (from MainActivity class)
public void setupDrawing() {
mPath = new Path();
paintLine = new Paint();
paintLine.setAntiAlias(true);
paintLine.setDither(true);
paintLine.setStrokeJoin(Paint.Join.ROUND);
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setColor(paintColor);
paintLine.setStrokeWidth(1);
paintLine.setStrokeCap(Paint.Cap.ROUND);
paintLine.setXfermode(null);
paintLine.setAlpha(255);
canvasPaint = new Paint(Paint.DITHER_FLAG);
lastBrushSize = paintLine.getStrokeWidth();
}
One more problem is that whenever I draw the first line when starting the app, it never shows the line that is being traced. Only once I finish drawing it is shows up then disappears. The next one I make is visible nicely as I trace it , it also disappears once its done.
Any tips on how to make all the lines remain on canvas and how to make my first line being visible while it is being drawn ?