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

标签 android android-button

我有一个带有几个圆形图像按钮的菜单。 现在按钮是垂直排列的 线性布局。我想做的是放置 圆圈中的按钮。按键数量 是可变的:

enter image description here

我怎样才能对称地放置可变数量的 沿着一个圆圈的按钮?稍后按钮将 可拖动,当一个按钮被拖入 圆心表示 Activity 开始。

最佳答案

下面是自定义圆圈布局的一小段代码:

    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

关于android - 将可变数量的按钮放在一个圆圈中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26798271/

相关文章:

java - 在 AsyncTask 执行方法中传递单选按钮代码

android 如何在单击按钮时导航网站

android - 设置一个按钮作为后退按钮

android - 如何重新启动模拟器以测试 ACTION_BOOT_COMPLETED?

javascript - 从 angular2 构建的新 Cordova 在 android 上构建白屏

java - 如何自动生成 API 版本之间的差异?

android - 如何在 Material Components 的 Material Button 中设置渐变背景?

android - 如何使用默认的 android Button 可绘制作为另一个 View 的背景?

Android - 使用 xml 制作箭头形状

Android 7.0 Doze 是否停止所有通知?