首页 > 解决方案 > 带圆角的六边形 ImageView

问题描述

我正在尝试制作一个带有圆角的六边形图像视图,这得益于我找到的一些代码并使用弧线来绘制角。这种方法不起作用可能是因为我做错了什么。我也希望有更简单的方法。
这是我的第一个圆角的代码,但结果并不好。

private void calculatePath(float radius) {
    float halfRadius = radius / 2f;
    float triangleHeight = (float) (Math.sqrt(3.0) * halfRadius);
    float centerX = getMeasuredWidth() / 2f;
    float centerY = getMeasuredHeight() / 2f;

    this.hexagonPath.reset();
    this.hexagonPath.moveTo(centerX, centerY + radius);
    this.hexagonPath.lineTo(centerX - triangleHeight / 4f * 3f, centerY + radius / 8f * 5f);
    this.hexagonPath.arcTo(new RectF(centerX - triangleHeight, centerY + radius / 8f * 5f, centerX - triangleHeight / 4f * 3f, centerY + halfRadius / 2f), 30f, 180f);
    this.hexagonPath.lineTo(centerX - triangleHeight, centerY - halfRadius);
    this.hexagonPath.lineTo(centerX, centerY - radius);
    this.hexagonPath.lineTo(centerX + triangleHeight, centerY - halfRadius);
    this.hexagonPath.lineTo(centerX + triangleHeight, centerY + halfRadius);
    this.hexagonPath.close();

    invalidate();
}

如您所见,这是没有任何弧的结果:

请帮助我找到解决方案或更简单的方法。

另外请不要链接我的库,我想自己为我的应用程序执行此操作。

对不起我的英语,不是我的主要语言。

标签: javaandroid

解决方案


推荐阅读