Home » Java » Android, scrolling points around a circle axis

Android, scrolling points around a circle axis

Posted by: admin September 18, 2018 Leave a comment


I’ll try my best to explain the issue I am having.

I have a listview of items.

The following function is triggered whenever scrollHorizontally is run.

Function setChildOffsets

private void setChildOffsets(@Dimension int radius, Point center, int peekDistance) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            final int yOffset = (int) resolveOffsetY(radius, child.getX() + child.getWidth() / 2, center, i);
            final int y = getHeight() - yOffset - child.getHeight();
            child.layout(child.getLeft(), y, child.getRight(), child.getHeight() + y);

As you can see, the function is going through each item of the list visible on screen. It then sets a y Offset. I suspect this is where the issue is.
See below the resolveOffsetY function:

Function resolveOffsetY

private double resolveOffsetY(double radius, double viewX, Point center, int child) {
        final double adjacent = Math.abs(center.x - viewX);
        final double radiusSquared = radius * radius;
        final double adjacentSquared = adjacent * adjacent;
        final double oppositeSideLength = Math.sqrt(radiusSquared - adjacentSquared);
        return oppositeSideLength;

This function basically retrieves the Y distance at which each item should be. Mathematically speaking, we used Pythagora’s, using the hypotenuse (radiusSquared) and the adjacent (adjacentSquared) to find the opposite side, which is the “height” of the item.

Back to setChildOffsets, once we have the height at which the item should be, we calculate its actual height within the display by subtracting the value we got from the height of the layout.

Finally, we set the child in the layout.

The issue is that the angle between items are unequal and produce an unwanted effect, you can see the two items on the right and left are further apart from their neighbor then the other items. The red circle is the center around which the items rotate. https://imgur.com/a/6Xtp19c

I’m bad at math, but i’m sure there must be a way to make these items move in a uniform circular manner around the circle so as to maintain an equal angle between the items.

I’d appreciate any help.