gpt4 book ai didi

android - 自定义 View Android 上错误的笔划宽度绘图圆

转载 作者:行者123 更新时间:2023-11-29 01:26:12 24 4
gpt4 key购买 nike

我正在 android 中编写自定义 View 。我想画一个覆盖我 View 所有宽度和高度的圆。这是我的代码

private void init() {

bgpaint = new Paint();
bgpaint.setColor(bgColor);
bgpaint.setAntiAlias(true);
bgpaint.setStyle(Paint.Style.STROKE);
bgpaint.setStrokeWidth(strokeWidth);
rect = new RectF();

}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// draw background circle anyway
int strokeWidth = 50;
rect.set(strokeWidth, strokeWidth, getwidth()- strokeWidth,
getheight() - strokeWidth);
canvas.drawArc(rect, -90, 360, fill, bgpaint);

}

但是当我运行时结果会是这样

enter image description here

我想要这样

enter image description here

我的代码有什么问题?

最佳答案

问题在于设置矩形的坐标。您必须设置笔画宽度的一半,而不是整个笔画宽度。

    float left=strokeWidth / 2;
float top=strokeWidth / 2;
float right=minDimen - strokeWidth/ 2;
float bottom=minDimen - strokeWidth / 2;
rect.set(left, top, right,bottom);

因为涉及到圆形,我假设您的 circleView 的宽度和高度是相同的尺寸。那是我代码中的 minDimen。因为我使用了这两个中较小的尺寸。

final int minDimen = Math.min(width, height);
setMeasuredDimension(minDimen, minDimen);

(必须在onMeasure方法中调用)无论如何,将宽度和高度设置为相同的尺寸是个好方法。

关于android - 自定义 View Android 上错误的笔划宽度绘图圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33953522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com