Dynamically arrange Buttons around a Circle

假如想象 提交于 2019-12-29 04:19:24

问题


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.


回答1:


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?




回答2:


The following Apache 2.0 Licensed project may be of service: https://github.com/dmitry-zaitsev/CircleLayout




回答3:


Instead of absolute layout you can use RelativeLayout and to child view, set the top and the left margins. Something like this:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
params.leftMargin = x;
params.topMargin = y;


来源:https://stackoverflow.com/questions/9368863/dynamically-arrange-buttons-around-a-circle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!