gpt4 book ai didi

java - 黑线意外出现在错误的地方

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

我使用 Canvas 创建了这幅画,但在这样做之后,出于某种原因,我很烦人地在最左边的红色矩形和包含数字 1 的灰色矩形之间出现了一条黑线。可以做些什么来摆脱它行?

enter image description here

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

private TextPaint mTextPaint;

public RectangleTextView(Context context, AttributeSet attrs) {
super(context, attrs);
int valueInPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
int valueInSp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics());

mRedPaint.setColor(Color.RED);

mBackPaint.setAntiAlias(false);
mBackPaint.setColor(Color.BLACK);
mBackPaint.setStrokeWidth(valueInPx);
mBackPaint.setStyle(Paint.Style.STROKE);

mTextPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(Color.BLACK);
mTextPaint.setTextAlign(Paint.Align.CENTER);
mTextPaint.setTextSize(valueInSp);

String pageTitle = getResources().getString(R.string.app_name);
}

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

//draw end rectangles
int mSideRectWidth = 10;
canvas.drawRect(0, 0, mSideRectWidth, getHeight(), mRedPaint); //draw left end rectangle
canvas.drawRect(getWidth() - mSideRectWidth, 0, getWidth(), getHeight(), mRedPaint); //draw right end rectangle

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


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

//draw text views
canvas.drawText(Integer.toString(i + 1), (i * boxWidth) + (boxWidth / 2), ((canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2)), mTextPaint);
}
}
}

最佳答案

你不想要“第一个”黑线,所以跳过“0”索引..像这样:

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

for (int i = 0; i < 7; i++) {
//draw text views
canvas.drawText(Integer.toString(i + 1), (i * boxWidth) + (boxWidth / 2), ((canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2)), mTextPaint);
}

这应该有所帮助。

编辑:此外,您没有考虑左侧的红线,因此您不会“居中”(因为每边都有一条线,您只需将其减少 1/2):

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

for (int i = 0; i < 7; i++) {
//draw text views
canvas.drawText(Integer.toString(i + 1), (mSideRectWidth / 2) + (i * boxWidth) + (boxWidth / 2), ((canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2)), mTextPaint);
}

关于java - 黑线意外出现在错误的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32030266/

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