gpt4 book ai didi

java - Canvas 上的线条显得微弱

转载 作者:行者123 更新时间:2023-11-30 01:57:29 25 4
gpt4 key购买 nike

在创建下图后,我注意到由于某种原因,一些黑线模糊不清,而另一些则清晰可见。可以使用什么代码来确保灰色框之间的黑线的宽度恰好为 1dp,红色矩形的宽度恰好为 5dp?

enter image description here

public class RectangleTextView extends View {
private final Paint mBackPaint = new Paint();
private final Paint mRedPaint = new Paint();
private int mSideRectWidth = 10;

public RectangleTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mBackPaint.setColor(Color.BLACK);
mRedPaint.setColor(Color.RED);
}

@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (getWidth() == 0)
return;

//draw grey boxes
setBackgroundColor(Color.parseColor("#808080"));
int boxWidth = getWidth() / 7;

//draw black line
for (int i = 0; i < 7; i++) {
canvas.drawLine(mSideRectWidth + boxWidth * i, 0, mSideRectWidth + boxWidth * i, getHeight(), mBackPaint);
}

//draw left end rectangle
canvas.drawRect(0, 0, mSideRectWidth, getHeight(), mRedPaint);

//draw right end rectangle
canvas.drawRect(getWidth() - mSideRectWidth, 0, getWidth(), getHeight(), mRedPaint);
}
}

最佳答案

这就是初始化 Paint 对象的全部方式吗?我不知道默认构造函数使用的默认值。我通常明确地设置它们:取消 ANTI_ALIAS_FLAG,将样式设置为 Paint.Style.STROKE 并设置所需的笔划 width。您需要的某些值可能是默认值,我只是不知道。

在处理细线时,关闭 ANTI_ALIAS_FLAG 可能非常重要。

关于java - Canvas 上的线条显得微弱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31998992/

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