Android - 为菜单使用细分弧
Android - Subdivide Arc for Menu usage
我想在android中创建一个自定义菜单,它应该如下图所示。
菜单图片
我已经实现了创建菜单的主要部分,不包括菜单点。所以看起来像下图。
菜单的当前状态
此菜单是一个 CustomView,我使用 canvas.drawArc 方法将这些圆弧绘制为 RectF。
所以我认为这很明显,我想要实现的目标。我想将 Arc 细分为大小均匀的更小的弧线,或者添加新的弧线(正好在另一个弧线上)。
我试图简单地将这些弧线的边界设置为较大弧线的 1/3,但是,我无法通过这种方式获得此结果。
有没有人知道这样做的方法,或者我完全改变我的方法?
好的。经过一些研究、实验和简单的数学计算,我得出了以下代码。
private void drawMenu(Canvas canvas, RectF arcBoundaries, int arcAngle, int sweep, Paint arcPaint, MenuPosition position, double scaleValue, int strokeWidth) {
int countAllMenuItems = getMenuCount(position);
int newAngle = arcAngle;
int counter = 0;
for (MenuItemView menuItem : menuItems) {
if (menuItem.getPosition().toLowerCase().equals(position.toString().toLowerCase())) {
canvas.drawArc(arcBoundaries, newAngle, sweep / countAllMenuItems, false, arcPaint);
newAngle += sweep / countAllMenuItems;
if (counter != countAllMenuItems - 1) {
calculateLines(arcBoundaries, newAngle, strokeWidth);
}
counter++;
}
}
}
}
private void calculateLines(RectF arcBoundaries, int angle, int strokeWidth) {
int realAngle = angle % 360;
float xRadius = arcBoundaries.width()/2;
float yRadius = arcBoundaries.height()/2;
double x = arcBoundaries.centerX() + (xRadius-(strokeWidth/2))*Math.cos(realAngle * Math.PI / 180);
double y = arcBoundaries.centerY() + (yRadius-(strokeWidth/2))*Math.sin(realAngle * Math.PI / 180);
double xEnd = arcBoundaries.centerX() + (xRadius+(strokeWidth/2))*Math.cos(realAngle * Math.PI / 180);
double yEnd = arcBoundaries.centerY() + (yRadius+(strokeWidth/2))*Math.sin(realAngle * Math.PI / 180);
lineList.put(new Point((int) x, (int) y), new Point((int) xEnd, (int) yEnd));
}
其实很简单。通过所有 MenuItems 的计数和扫描角度,我计算出每条线的角度。然后我使用角度函数计算 x 和 y 的起始值和结束值。
所以我使用公式:
x(相邻边)= centerX(调整到坐标系)+斜边*cos(角度)
y(对边)=centerY(调整到坐标系)+斜边*sin(角度)
值得一提的是,角度函数中的角度必须采用弧度度量。另外,我必须 subtract/add 有描边,因为我使用描边样式来绘制这些弧,这意味着半径到达描边的中心。
我将这些值放在一个列表中,然后在绘制完其他所有内容后绘制它们,因此它们高于其他所有内容。
希望能帮到遇到同样问题的大家
我想在android中创建一个自定义菜单,它应该如下图所示。
菜单图片
我已经实现了创建菜单的主要部分,不包括菜单点。所以看起来像下图。
菜单的当前状态
此菜单是一个 CustomView,我使用 canvas.drawArc 方法将这些圆弧绘制为 RectF。 所以我认为这很明显,我想要实现的目标。我想将 Arc 细分为大小均匀的更小的弧线,或者添加新的弧线(正好在另一个弧线上)。 我试图简单地将这些弧线的边界设置为较大弧线的 1/3,但是,我无法通过这种方式获得此结果。
有没有人知道这样做的方法,或者我完全改变我的方法?
好的。经过一些研究、实验和简单的数学计算,我得出了以下代码。
private void drawMenu(Canvas canvas, RectF arcBoundaries, int arcAngle, int sweep, Paint arcPaint, MenuPosition position, double scaleValue, int strokeWidth) {
int countAllMenuItems = getMenuCount(position);
int newAngle = arcAngle;
int counter = 0;
for (MenuItemView menuItem : menuItems) {
if (menuItem.getPosition().toLowerCase().equals(position.toString().toLowerCase())) {
canvas.drawArc(arcBoundaries, newAngle, sweep / countAllMenuItems, false, arcPaint);
newAngle += sweep / countAllMenuItems;
if (counter != countAllMenuItems - 1) {
calculateLines(arcBoundaries, newAngle, strokeWidth);
}
counter++;
}
}
}
}
private void calculateLines(RectF arcBoundaries, int angle, int strokeWidth) {
int realAngle = angle % 360;
float xRadius = arcBoundaries.width()/2;
float yRadius = arcBoundaries.height()/2;
double x = arcBoundaries.centerX() + (xRadius-(strokeWidth/2))*Math.cos(realAngle * Math.PI / 180);
double y = arcBoundaries.centerY() + (yRadius-(strokeWidth/2))*Math.sin(realAngle * Math.PI / 180);
double xEnd = arcBoundaries.centerX() + (xRadius+(strokeWidth/2))*Math.cos(realAngle * Math.PI / 180);
double yEnd = arcBoundaries.centerY() + (yRadius+(strokeWidth/2))*Math.sin(realAngle * Math.PI / 180);
lineList.put(new Point((int) x, (int) y), new Point((int) xEnd, (int) yEnd));
}
其实很简单。通过所有 MenuItems 的计数和扫描角度,我计算出每条线的角度。然后我使用角度函数计算 x 和 y 的起始值和结束值。
所以我使用公式:
x(相邻边)= centerX(调整到坐标系)+斜边*cos(角度)
y(对边)=centerY(调整到坐标系)+斜边*sin(角度)
值得一提的是,角度函数中的角度必须采用弧度度量。另外,我必须 subtract/add 有描边,因为我使用描边样式来绘制这些弧,这意味着半径到达描边的中心。
我将这些值放在一个列表中,然后在绘制完其他所有内容后绘制它们,因此它们高于其他所有内容。
希望能帮到遇到同样问题的大家