将可变数量的按钮放在一个圆圈中

时间:2014-11-07 09:41:33

标签: android android-button

我有一个带有几个圆形图像按钮的菜单。 现在按钮是垂直排序的 一个LinearLayout。我想做的就是放置 圆圈中的按钮。按钮的数量 是可变的:

enter image description here

我如何对称放置一个可变数量的 沿着一个圆圈的按钮?后来按钮会 是可拖动的,当拖动按钮时 活动开始的圆心。

1 个答案:

答案 0 :(得分:1)

以下是自定义圈子布局的小代码片段:

    private RelativeLayout.LayoutParams modifyLayoutParams(
            RelativeLayout.LayoutParams lp, int degree) {
        /**
         * Determine in Quadrant or on Axis
         * Using Android convention. Right X-axis is degree 0, growing clockwise.
         * */
        degree = degree % 360;
        if (degree < 0) { // Make it positive
            degree += 360;
        }
        if (degree == 0) { // right x-axis. Right
            lp.addRule(RelativeLayout.RIGHT_OF, R.id.center);
            lp.addRule(RelativeLayout.CENTER_VERTICAL);
            lp.setMargins(radius, 0, 0, 0);
        } else if (degree > 0 && degree < 90) { // Quadrant IV. Lower Right
            lp.addRule(RelativeLayout.BELOW, R.id.center);
            lp.addRule(RelativeLayout.RIGHT_OF, R.id.center);
            // Determine margin. 
            lp.setMargins(getMarginX(degree), getMarginY(degree), 0, 0); 
        } else if (degree == 90) { // Bottom y-axis. Bottom
            lp.addRule(RelativeLayout.BELOW, R.id.center); // Above Center.
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            lp.setMargins(0, radius, 0, 0);
        } else if (degree > 90 && degree < 180) { // Quadrant III. Lower Left
            lp.addRule(RelativeLayout.BELOW, R.id.center);
            lp.addRule(RelativeLayout.LEFT_OF, R.id.center);
            // Determine margin. 
            lp.setMargins(0, getMarginX(degree - 90), getMarginY(degree - 90), 0);
        } else if (degree == 180) { // Right x-axis. Left
            lp.addRule(RelativeLayout.LEFT_OF, R.id.center);
            lp.addRule(RelativeLayout.CENTER_VERTICAL);
            lp.setMargins(0, 0, radius, 0);
        } else if (degree > 180 && degree < 270) { // Quadrant II. Upper Left
            lp.addRule(RelativeLayout.ABOVE, R.id.center);
            lp.addRule(RelativeLayout.LEFT_OF, R.id.center);
            // Determine margin. 
            lp.setMargins(0, 0, getMarginX(degree - 180), getMarginY(degree - 180));
        } else if (degree == 270) { // Top y-axis. Top
            lp.addRule(RelativeLayout.ABOVE, R.id.center); // Above Center.
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            lp.setMargins(0, 0, 0, radius);
        } else if (degree > 270 && degree < 360) { // Quadrant I. Upper Right
            lp.addRule(RelativeLayout.ABOVE, R.id.center);
            lp.addRule(RelativeLayout.RIGHT_OF, R.id.center);
            // Determine margin. 
            lp.setMargins(getMarginX(360 - degree), 0, 0, getMarginY(360 - degree));
        }
        return lp;
    }
    /** X offset i.e. adjacent length */
    private int getMarginX(int degree) {
        return Math.abs((int)(Math.cos(Math.toRadians(degree)) * radius));
    }
    /** Y offset i.e. opposite length */
    private int getMarginY(int degree) {
        return Math.abs((int)(Math.sin(Math.toRadians(degree)) * radius));
    }
}

Here是完整的帖子。上面的代码基本上告诉我们如何自定义布局并在那里放置控件。

输出是:

enter image description here