Wednesday, 4 September 2019

Android: Drawing lines using onDraw and onTouchEvent render slow

I am trying to draw four lines using the ontouch method and onDraw method like below:

 @Override
       protected void onDraw(Canvas canvas) {
        initPoints();
        path.reset();
        path.addCircle(topLeftPoint.x,topLeftPoint.y, 16f,       
       Path.Direction.CW);
        canvas.drawLines(points, paint);

        path.addCircle(middleTopPoint.x, middleTopPoint.y, 16f, Path.Direction.CW);
        path.addCircle(topRightPoint.x, topRightPoint.y, 16f, Path.Direction.CW);

//      canvas.drawLine(topRightPoint.x, topRightPoint.y, bottomRightPoint.x, bottomRightPoint.y, paint);
        path.addCircle(bottomRightPoint.x, bottomRightPoint.y, 16f, Path.Direction.CW);

        path.addCircle(middleRightPoint.x, middleRightPoint.y, 16f, Path.Direction.CW);
//      canvas.drawLine(bottomRightPoint.x, bottomRightPoint.y, bottomLeftPoint.x, bottomLeftPoint.y, paint);
        path.addCircle(bottomLeftPoint.x, bottomLeftPoint.y, 16f, Path.Direction.CW);
        path.addCircle(middleBottomPoint.x, middleBottomPoint.y, 16f, Path.Direction.CW);

//      canvas.drawLine(bottomLeftPoint.x, bottomLeftPoint.y, topLeftPoint.x, topLeftPoint.y, paint);
        path.addCircle(topRightPoint.x, topRightPoint.y, 16f, Path.Direction.CW);
        path.addCircle(middleLeftPoint.x, middleLeftPoint.y, 16f, Path.Direction.CW);
        path.close();
        canvas.drawPath(path, paint);
            }

    @Override
    public boolean onTouchEvent(final MotionEvent event) {

        int action = event.getActionMasked();

        if (event.getAction() == MotionEvent.ACTION_DOWN ||
                event.getAction() == MotionEvent.ACTION_MOVE
                || event.getAction() == MotionEvent.ACTION_UP) {

            float xCoordinate = 16;
            float yCoordinate = 16;

            if (event.getX() < 16) {
                xCoordinate = 16;
            } else if (event.getX() > getWidth() - 16) {
                xCoordinate = getWidth() - 16;
            } else {
                xCoordinate = event.getX();
            }

            if (event.getY() < 16) {
                yCoordinate = 16;
            } else if (event.getY() > getHeight() - 16) {
                yCoordinate = getHeight() - 16;
            } else {
                yCoordinate = event.getY();
            }
            if (isInsideTopLeft(event.getX(), event.getY())) {
                topLeftPoint.set(xCoordinate, yCoordinate);

                middleTopPoint.set((topRightPoint.x + topLeftPoint.x) / 2,
                        (topRightPoint.y + topLeftPoint.y) / 2);
                middleLeftPoint.set((bottomLeftPoint.x + topLeftPoint.x) / 2,
                        (bottomLeftPoint.y + topLeftPoint.y) / 2);
                invalidate();
                return true;
            } else if (isInsideTopRight(event.getX(), event.getY())) {
                topRightPoint.set(xCoordinate, yCoordinate);

                middleTopPoint.set((topRightPoint.x + topLeftPoint.x) / 2,
                        (topRightPoint.y + topLeftPoint.y) / 2);
                middleRightPoint.set((bottomRightPoint.x + topRightPoint.x) / 2,
                        (bottomRightPoint.y + topRightPoint.y) / 2);
                invalidate();
                return true;
            } else if (isInsideBottomRight(event.getX(), event.getY())) {
                bottomRightPoint.set(xCoordinate, yCoordinate);

                middleBottomPoint.set((bottomLeftPoint.x + bottomRightPoint.x) / 2,
                        (bottomLeftPoint.y + bottomRightPoint.y) / 2);
                middleRightPoint.set((bottomRightPoint.x + topRightPoint.x) / 2,
                        (bottomRightPoint.y + topRightPoint.y) / 2);
                invalidate();
                return true;
            } else if (isInsideBottomLeft(event.getX(), event.getY())) {
                bottomLeftPoint.set(xCoordinate, yCoordinate);

                middleBottomPoint.set((bottomLeftPoint.x + bottomRightPoint.x) / 2,
                        (bottomLeftPoint.y + bottomRightPoint.y) / 2);
                middleLeftPoint.set((bottomLeftPoint.x + topLeftPoint.x) / 2,
                        (bottomLeftPoint.y + topLeftPoint.y) / 2);
                invalidate();
                return true;
            } 
        }

        return super.onTouchEvent(event);
    }   

but when i am increasing the touching speed the render became slower. I already used paths and lines but the increasing performance is still small.

The rest of methods i removed from the code such as declaring the PointF objects and initiating those objects with default values.

If someone have any idea i will appreciate.

Best Regards, AurelianR



from Android: Drawing lines using onDraw and onTouchEvent render slow

No comments:

Post a Comment