我有一个带有几个圆形图像按钮的菜单。 现在按钮是垂直排列的 线性布局。我想做的是放置 圆圈中的按钮。按键数量 是可变的:
我怎样才能对称地放置可变数量的 沿着一个圆圈的按钮?稍后按钮将 可拖动,当一个按钮被拖入 圆心表示 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是完整的职位。上面的代码基本上告诉我们如何自定义布局和放置控件。
输出是:
关于android - 将可变数量的按钮放在一个圆圈中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26798271/