如何使用画布绘制带角半径框的线

发布于 2025-01-18 10:54:38 字数 1737 浏览 3 评论 0原文

我想用盒子角半径绘制盒子线。

预期输出

但是我得到了什么

代码

@Override
    protected void onDraw(Canvas canvas) {
        if (bytes != null) {
            int[] gradientColors = new int[]{
                    Color.parseColor("#B3F90403"),
                    Color.parseColor("#B3E7D104"),
                    Color.parseColor("#B317A300")
            };

            float[] gradientColorPos = new float[]{
                    0.33f, 0.66f, 0.99f
            };

            paint.setShader(new LinearGradient(0, 0, 0f, getHeight() / 2, gradientColors, gradientColorPos, Shader.TileMode.MIRROR));


            float barWidth = getWidth() / density;
            float div = bytes.length / density;
            paint.setStrokeWidth(barWidth - gap);
            for (int i = 0; i < density; i++) {
                int count = 0;
                int bytePosition = (int) Math.ceil(i * div);
                int top = getHeight() + ((byte) (Math.abs(bytes[bytePosition]) + 128)) * getHeight() / 128;
                int col = Math.abs((getHeight() - top));
                for (int j = 0; j < col + 1; j += barWidth) {
                    float barX = (i * barWidth) + (barWidth / 2);
                    float y1 = getHeight() - ((barWidth + (gap / 2f)) * count);
                    float y2 = getHeight() - ((barWidth - gap / 2f) + ((barWidth + gap / 2f) * count));
                    canvas.drawLine(barX, y1, barX, y2, paint);
                    count++;
                }
            }
            super.onDraw(canvas);
        }
    }

I want to draw line of box with box corner radius.

Expected Output

But what i get

Code

@Override
    protected void onDraw(Canvas canvas) {
        if (bytes != null) {
            int[] gradientColors = new int[]{
                    Color.parseColor("#B3F90403"),
                    Color.parseColor("#B3E7D104"),
                    Color.parseColor("#B317A300")
            };

            float[] gradientColorPos = new float[]{
                    0.33f, 0.66f, 0.99f
            };

            paint.setShader(new LinearGradient(0, 0, 0f, getHeight() / 2, gradientColors, gradientColorPos, Shader.TileMode.MIRROR));


            float barWidth = getWidth() / density;
            float div = bytes.length / density;
            paint.setStrokeWidth(barWidth - gap);
            for (int i = 0; i < density; i++) {
                int count = 0;
                int bytePosition = (int) Math.ceil(i * div);
                int top = getHeight() + ((byte) (Math.abs(bytes[bytePosition]) + 128)) * getHeight() / 128;
                int col = Math.abs((getHeight() - top));
                for (int j = 0; j < col + 1; j += barWidth) {
                    float barX = (i * barWidth) + (barWidth / 2);
                    float y1 = getHeight() - ((barWidth + (gap / 2f)) * count);
                    float y2 = getHeight() - ((barWidth - gap / 2f) + ((barWidth + gap / 2f) * count));
                    canvas.drawLine(barX, y1, barX, y2, paint);
                    count++;
                }
            }
            super.onDraw(canvas);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文