动态安排一圈按钮 [英] Dynamically arrange Buttons around a Circle

查看:126
本文介绍了动态安排一圈按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是新到Android所以我不完全熟悉所有的视图组件。 我挣扎着一圈动态调整按钮。

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.

我所试图实现的是加入正按钮(n可以在创建时更改),以一种观点认为,看起来像连接图像(http://i.stack.imgur.com/NYQeR.jpg)。我想避免使用absoluteLayout(但我愿意接受建议如果这是解决这个问题的唯一途径)。 我已经想出了一个计算的按钮的X / Y位置(忽略按钮的大小现在):

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 (http://i.stack.imgur.com/NYQeR.jpg). 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?
  }

我想过用这个code由内自定义视图但是从我所看到的视图需要从一个ViewGroup子类有addView方法,然后再只absoluteLayout似乎允许设置的x,y位置......我在如何实现这一功能的丧失。

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.

我会添加一些动画的看法以后,因此使用SurfaceView可能是很好,如果这是可能的,但它不是必需的。

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.

我创造我自己的观点继承RelativeLayout的。在的onCreate()我设置

I create my own view subclassing RelativeLayout. In onCreate() I set

setWillNotDraw(false);

这样的OnDraw()被调用。 然后,我继续的OnDraw():

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 );
  }

这给了我想要的结果,但是的LayoutParams的初始化感觉(最有可能的是)不对的。有没有更好的方式来做到这一点?

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?

这篇关于动态安排一圈按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆