Bezier Curve Drawing Android Drawing Application Very Slow

I am working on a drawing android application that is for drawing a bezier line based on user interaction. The user draws a line on the screen and it appears exactly as they drew it.

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

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            drawingElement = new DrawingElement(gridType, canvasWidth, canvasHeight);
            drawingElement.addPoint(touchX, touchY);
            break;
        case MotionEvent.ACTION_MOVE:
            drawingElement.addPoint(touchX, touchY);
            break;
        case MotionEvent.ACTION_UP:
            drawingElement.addPoint(touchX, touchY);

                actualDrawingElement = new DrawPath(gridType, canvasWidth, canvasHeight, drawPaint, paintColor, brushSize);
                actualDrawingElement.setDrawingElementType(DrawingElementType.PATH);

            actualDrawingElement.setPoints(drawingElement.getPoints());
            drawingElement = actualDrawingElement;
            actualDrawingElement = null;
            finishedElement = true;

            break;
        default:
            return false;
    }




//draw the view - will be called after touch event
@Override
protected void onDraw(Canvas canvas) {

    if(bgPaint == null) {
        bgPaint = new Paint();
        bgPaint.setColor(Color.WHITE);
    }
    canvas.drawRect(0, 0, getWidth(), getHeight(), bgPaint);

    Bitmap dbBMP =  null;
    BitmapSave bitmapSave = getDatabaseHandler().getLastSave();
    if(bitmapSave != null) {
        dbBMP = bitmapSave.getBitmap().copy(Bitmap.Config.ARGB_8888, true);
    } else {
        dbBMP = Bitmap.createBitmap((int) getActualWidth(), (int) getActualHeight(), Bitmap.Config.ARGB_8888);
    }
    Canvas dbCanvas = new Canvas(dbBMP);

    DrawPath p;
    if(drawingElement != null && drawingElement.getPointCount() > 0) {
        actualDrawingElement = new DrawPath(gridType, canvasWidth, canvasHeight, drawPaint, paintColor, brushSize);
        actualDrawingElement.setDrawingElementType(DrawingElementType.PATH);
        actualDrawingElement.setPoints(drawingElement.getPoints());
        p = (DrawPath)actualDrawingElement;
        p.draw(dbCanvas);
        p = null;

    }

    if(finishedElement) {
        db.addSave(new BitmapSave(dbBMP));
    }
    canvas.drawBitmap(dbBMP, 0, 0, drawPaint);

    if(displayGrid) {
        DrawGrid drawGrid = new DrawGrid(gridType, canvasWidth, canvasHeight, bgPaint);
        drawGrid.draw(canvas);
    }

    updateUndoRedoButtons();
}

      

The above code shows my onDraw and onTouchEvent methods. I switched to using a database for storing bitmaps as I started getting a lot of errors on memory overload and this would work. I used to have multiple in-memory bitmaps to undo / redo inside an ArrayList (before failing to 3 or 4). I would like to have at least 20 undo / redo (total) in the application.

In the end, it is designed to draw multiple lines at the same time, but even one line is slow.

I tried to distract myself from a few things, for example. drawing a line ... It stores the points in a separate class which it takes over later.

Abstract line drawing method ...

public void draw(Canvas canvas) {

    boolean firstRun = true;

    Point2D prevPoints = null;
    Point2D points = null;
    for(Point2D pointGrp : getPoints()) {
        points = getCalculatedPoints(pointGrp);
            if (firstRun) {
                paths.add(new Path());
                paths.get(0).moveTo(points.getPosX(), points.getPosY());
            } else {
                paths.get(0).quadTo(prevPoints.getPosX(), prevPoints.getPosY(), points.getPosX(), points.getPosY());
            }
        firstRun = false;
        prevPoints = points;
    }

    canvas.drawPath(paths.get(0), getPaint());
}

      

At this stage, "points = getCalculatedPoints (pointGrp)"; literally returns the exact same point it gives. The Point2D object just holds pointx and points nothing else. Has no methods other than getters and setters.

Any ideas why this would be very slow? It only lifts 4 points along a massive line, making it very jagged and straight.

+3


source to share





All Articles