【问题标题】:Dynamically arrange Buttons around a Circle围绕圆圈动态排列按钮
【发布时间】:2012-02-20 21:43:54
【问题描述】:

我还是 android 新手,所以我对所有视图组件并不完全熟悉。 我正在努力围绕一个圆圈动态对齐按钮。

我想要实现的是将 n 个按钮(n 可以在创建时更改)添加到一个看起来像附加图像的视图:

我想避免使用 absoluteLayout(但如果这是解决它的唯一方法,我愿意接受建议)。 我已经计算出了按钮的 x/y 位置(暂时忽略按钮大小):

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?
  }

我曾考虑在自定义视图中使用此代码,但从我所见,该视图需要从 ViewGroup 子类化以具有 addView 方法,然后再次似乎只有 absoluteLayout 允许设置 x、y 位置...我不知道如何实现这个功能。

稍后我可能会向该视图添加一些动画,因此如果可能的话,使用 SurfaceView 可能会很好,但这不是必需的。

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    我想我找到了我试图实现的解决方案。

    我创建自己的视图子类化 RelativeLayout。在 onCreate() 我设置了

    setWillNotDraw(false);
    

    这样 onDraw() 就会被调用。 然后我继续 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 的初始化感觉(并且很可能是)错误。有没有更好的方法来做到这一点?

    【讨论】:

    • 我在使用这种方法时遇到了另一个问题。 onDraw() 被反复调用,这不是我在那里设置按钮时想要的。我想我可能可以用一个 if 和一个布尔值来包围初始化,但是有没有一种可接受的方式来实现我想要的行为? (例如,使用按钮初始化我的自定义视图,从中心以圆形布置,而不使用 onDraw?)
    【解决方案2】:

    以下 Apache 2.0 许可项目可能会提供服务:https://github.com/dmitry-zaitsev/CircleLayout

    【讨论】:

    • 我想要按钮点击事件,但我找不到。
    【解决方案3】:

    您可以使用RelativeLayout 代替绝对布局,并为子视图设置顶部和左侧边距。像这样的:

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

    【讨论】:

    • 这是个好主意,你的建议实现了我想要做的。我为此将RelativeLayout 子类化并在onCreate 中创建按钮。他们的 LayoutParams(以及来自视图本身的那些)在这一点上是空的。初始化它们的首选方法是什么?我忘了提到的另一个要求是圆圈应该位于视图的中心。我认为只需添加从 getHeight() 和 getWidth() 获取的值来偏移位置会很容易,但是这两种方法都返回 0(getMeasuredWidth() 和 Height 也是如此)。有什么办法可以解决吗?
    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 2021-06-24
    • 2013-06-19
    • 1970-01-01
    • 2019-09-04
    • 2019-04-25
    • 1970-01-01
    相关资源
    最近更新 更多