如何使用画布绘制带角半径框的线
我想用盒子角半径绘制盒子线。
预期输出

但是我得到了什么

代码
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论