I'm still new to android so I'm not totally familiar with all the view components.
I'm struggling with aligning Buttons dynamically around a circle.
What I am trying to achieve is to add n buttons (n can change at creation time) to a view that looks like the attached image:
I'd like to avoid using absoluteLayout (but I'm open to suggestions if that's the only way to solve it).
I already came up with a calculation for the x/y positions for the buttons (ignoring button size for now):
int iNumberOfButtons = 10;
double dIncrease = Math.PI * 2 / iNumberOfButtons,
dAngle = 0,
x = 0,
y = 0;
for( int i = 0; i < iNumberOfButtons; i++ )
{
x = 100 * Math.cos( dAngle ) + 200;
y = 100 * Math.sin( dAngle ) + 200;
dAngle += dIncrease;
// get button and set position?
}
I thought about using this code from inside a custom view but from what I've seen the view needs to be subclassed from ViewGroup to have the addView method and then again only absoluteLayout seems to allow setting x, y positions... I'm at a loss how to implement this feature.
I might add some animations to that view later on, so using SurfaceView might be nice if it's possible but it's not a requirement.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…