metRo_
metRo_

Reputation: 443

Android - Save path and draw later

I'm using the following code to get a touch event, draw the path and store it on an ArrayList and it is working.

@Override
public boolean onTouchEvent(MotionEvent event) {
    float touchX = event.getX();
    float touchY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        drawPath.moveTo(touchX, touchY);
        break;
    case MotionEvent.ACTION_MOVE:
        drawPath.lineTo(touchX, touchY);
        drawCanvas.drawPath(drawPath, drawPaint);
        paths.add(drawPath);
        drawPath.reset();
        drawPath.moveTo(touchX, touchY);
        break;
    case MotionEvent.ACTION_UP:
        drawCanvas.drawPath(drawPath, drawPaint);
        paths.add(drawPath);
        drawPath.reset();
        break;
    default:
        return false;
    }

    invalidate();
    return true;
}

After I want to draw it again but with a diferent color and it doesn't work. If I create the path, as commented, it works :s

public void printPath(){

    Path testePath = new Path();
    //testePath.moveTo(0, 0);
    //testePath.lineTo(300, 300);

    Paint testePaint = new Paint();
    testePaint.setColor(0xFF00FF00);
    testePaint.setAntiAlias(true);
    testePaint.setStrokeWidth(brushSize);
    testePaint.setStyle(Paint.Style.STROKE);
    testePaint.setStrokeJoin(Paint.Join.ROUND);
    testePaint.setStrokeCap(Paint.Cap.ROUND);

    testePath = paths.remove(0);
    drawCanvas.drawPath(testePath, testePaint);
}

How I create the math:

public class DrawingView extends View {

    //drawing path
    private Path drawPath;
    //drawing and canvas paint
    private Paint drawPaint, canvasPaint;
    //initial color
    private int paintColor = 0x00660000;
    //canvas
    private Canvas drawCanvas;
    //canvas bitmap
    private Bitmap canvasBitmap;

    private float brushSize, lastBrushSize;

    private ArrayList<Path> paths;

    private boolean erase=false;

    public DrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupDrawing();
    }

    private void setupDrawing(){
        brushSize = getResources().getInteger(R.integer.medium_size);
        lastBrushSize = brushSize;

        //get drawing area setup for interaction
        drawPath = new Path();
        drawPaint = new Paint();

        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(brushSize);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
        drawPaint.setAlpha(150);

        canvasPaint = new Paint(Paint.DITHER_FLAG);

        paths = new ArrayList<Path>();
    }
    ...

Upvotes: 0

Views: 2728

Answers (1)

Pasquale Anatriello
Pasquale Anatriello

Reputation: 2355

It doesn't work because you keep resetting your path. Remove the drawPath.reset(); otherwise the path will be empty when you try to draw it.

Upvotes: 5

Related Questions