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
java android bitmap canvas bezier


source to share


No one has answered this question yet

Check out similar questions:

3295
Why is the Android emulator so slow? How can we speed up the development of an Android emulator?
685
Android separator / line separator in layout?
7
Canvas drawing draws a black line
2
How to draw a sound shape on Android
2
Raster path of a brush on canvas is not a smooth path
0
Canvas enlarge in custom view
0
Cancel drawing of android image editor?
0
Undo / Redo not working in android Canvas
0
Drawing bitmap to canvas after calling setBitmap not working
-1
android drawing gets leggy when painting with brushes and flowers



All Articles
Loading...
X
Show
Funny
Dev
Pics