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.
I think I found the solution I tried to achieve.
I create my own view subclassing RelativeLayout. In onCreate() I set
setWillNotDraw(false);
so that onDraw() gets called. I then continue in onDraw():
int iHeight = getHeight();
int iWidth = getWidth();
int iNumberOfButtons = 10;
double dIncrease = Math.PI * 2 / iNumberOfButtons,
dAngle = 0,
x = 0,
y = 0;
for( int i = 0; i < iNumberOfButtons; i++ )
{
x = 200 * Math.cos( dAngle ) + iWidth/2;
y = 200 * Math.sin( dAngle ) + iHeight/2;
dAngle += dIncrease;
Button xButton = new Button(m_xContext);
xButton.setAdjustViewBounds(true);
xButton.setBackgroundResource(R.drawable.some_image);
LayoutParams xParams = (RelativeLayout.LayoutParams)xButton.getLayoutParams();
if( xParams == null )
{
xParams = new RelativeLayout.LayoutParams( xButton.getBackground().getIntrinsicWidth(), xButton.getBackground().getIntrinsicHeight() );
}
xParams.leftMargin = (int)x - ( xButton.getBackground().getIntrinsicWidth() / 2 ) ;
xParams.topMargin = (int)y - ( xButton.getBackground().getIntrinsicHeight() / 2 );
addView( xButton, xParams );
}
This gives me the desired result, however the initializing of the LayoutParams feels (and most likely is) wrong. Is there a better way to do this?
The following Apache 2.0 Licensed project may be of service: https://github.com/dmitry-zaitsev/CircleLayout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With