0

I am trying to draw spiral view as in screenshot attached, I have array of points to draw based on number on circles. I am new to android and kotlin .

Can anyone please suggest how to do this?

enter image description here

Kishor
  • 376
  • 4
  • 14

1 Answers1

1

Today I added spiral drawing to this custom scrolling shapes app I'm building. My implementation makes use of Android import android.graphics.Path;. The spiral drawing is based on the user scrolling, with more segments added as the user scrolls.

The gist of the solution is as follows: I start with an initial arc and draw that. That arc is the initial arc of the spiral. On that arc has been added to the path, then use the bounding Rect of the Path to determine the bounds for the next segment of the arc.

The below is a sample but for full context you'll need to view the project: https://github.com/jdgreene2008/android_custom_views

         private void addTopSpiralSegment(Path path, RectF bounds, SpiralSegment spiralSegment) {
                float centerX = bounds.centerX();
                float centerY = bounds.centerY();

                float segmentBoundsLeft;
                float segmentBoundsRight;
                float segmentBoundsTop;
                float segmentBoundsBottom;

                if (path.isEmpty()) {
                    segmentBoundsLeft = centerX - spiralSegment.getWidth() / 2;
                    segmentBoundsRight = centerX + spiralSegment.getWidth() / 2;
                    segmentBoundsTop = centerY - spiralSegment.getHeight() / 2;
                    segmentBoundsBottom = centerY + spiralSegment.getHeight() / 2;
                } else {
                    RectF pathBounds = new RectF();
                    path.computeBounds(pathBounds, true);

                    segmentBoundsLeft = pathBounds.left;
                    segmentBoundsRight = segmentBoundsLeft + spiralSegment.getWidth();
                    segmentBoundsTop = centerY - spiralSegment.getHeight() / 2;
                    segmentBoundsBottom = centerY + spiralSegment.getHeight() / 2;
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    path.addArc(segmentBoundsLeft, segmentBoundsTop, segmentBoundsRight,
                            segmentBoundsBottom, 180, 180);
                }
            }

    private void addBottomSpiralSegment(Path path, RectF bounds, SpiralSegment spiralSegment) {
            float centerX = bounds.centerX();
            float centerY = bounds.centerY();

            float segmentBoundsLeft;
            float segmentBoundsRight;
            float segmentBoundsTop;
            float segmentBoundsBottom;

            if (path.isEmpty()) {
                segmentBoundsLeft = centerX - spiralSegment.getWidth() / 2;
                segmentBoundsRight = centerX + spiralSegment.getWidth() / 2;
                segmentBoundsTop = centerY - spiralSegment.getHeight() / 2;
                segmentBoundsBottom = centerY + spiralSegment.getHeight() / 2;
            } else {
                RectF pathBounds = new RectF();
                path.computeBounds(pathBounds, true);

                segmentBoundsLeft = pathBounds.right - spiralSegment.getWidth();
                segmentBoundsRight = pathBounds.right;
                segmentBoundsTop = centerY - spiralSegment.getHeight() / 2;
                segmentBoundsBottom = centerY + spiralSegment.getHeight() / 2;
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                path.addArc(segmentBoundsLeft, segmentBoundsTop, segmentBoundsRight,
                        segmentBoundsBottom, 0, 180);
            }
        }
Jarvis
  • 392
  • 1
  • 6