gpt4 book ai didi

java - Canvas.drawRect() 不绘制任何东西

转载 作者:行者123 更新时间:2023-12-02 02:23:36 25 4
gpt4 key购买 nike

我的 Canvas 类中的画家没有绘制矩形,尽管我将其配置为这样做。

我有一个简单的 Canvas 布局,并且设置了 6 个矩形画家,如果相应的 boolean 标志为 true,它们应该绘制一个灰色矩形。尽管如此,由于某种原因,即使给定的 boolean 值为 true,也不会绘制矩形。

如下面的屏幕截图所示(取自 onDraw 范围内),任何给定的矩形在运行时都有有效的坐标,因此它应该是可见的。在你问之前;是的,我已经验证了我的代码中的 canvas.drawRect() 调用已达到。他们根本不画任何东西。 enter image description here

我错过了什么?

下面是我的 Canvas 类。我是不是哪里搞砸了?

public class Canvas_StrumPad extends View {

private Paint eStringRectPainter, aStringRectPainter, dStringRectPainter, gStringRectPainter,
bStringRectPainter, eeStringRectPainter;
private int viewWidth, viewHeight, stringRectWidth;
public int eRectBoundary, aRectBoundary, dRectBoundary, gRectBoundary, bRectBoundary, eeRectBoundary;

public boolean eStringTouched = true, aStringTouched, dStringTouched = true, gStringTouched, bStringTouched,
eeStringTouched = true;

public Canvas_StrumPad(Context context) {
super(context);
initView();
}

public Canvas_StrumPad(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView();
}

public Canvas_StrumPad(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}

private void initView(){
eStringRectPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
eStringRectPainter.setColor(Color.BLACK);
eStringRectPainter.setAlpha(128);

aStringRectPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
aStringRectPainter.setColor(Color.parseColor("#616161"));
aStringRectPainter.setAlpha(128);

dStringRectPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
dStringRectPainter.setColor(Color.parseColor("#616161"));
dStringRectPainter.setAlpha(128);

gStringRectPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
gStringRectPainter.setColor(Color.parseColor("#616161"));
gStringRectPainter.setAlpha(128);

bStringRectPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
bStringRectPainter.setColor(Color.parseColor("#616161"));
bStringRectPainter.setAlpha(128);

eeStringRectPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
eeStringRectPainter.setColor(Color.parseColor("#616161"));
eeStringRectPainter.setAlpha(128);
}

public void setViewParams(int width, int height){
viewWidth = width;
viewHeight = height;

stringRectWidth = viewWidth/6;

eRectBoundary = stringRectWidth;
aRectBoundary = stringRectWidth*2;
dRectBoundary = stringRectWidth*3;
gRectBoundary = stringRectWidth*4;
bRectBoundary = stringRectWidth*5;
eeRectBoundary = stringRectWidth*6;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

if (eStringTouched){
canvas.drawRect(0,viewHeight,eRectBoundary,0,eStringRectPainter);
}
if (aStringTouched){
canvas.drawRect(eRectBoundary,viewHeight,aRectBoundary,0,aStringRectPainter);
}
if (dStringTouched){
canvas.drawRect(aRectBoundary,viewHeight,dRectBoundary,0,dStringRectPainter);
}
if (gStringTouched){
canvas.drawRect(dRectBoundary,viewHeight,gRectBoundary,0,gStringRectPainter);
}
if (bStringTouched){
canvas.drawRect(gRectBoundary,viewHeight,bRectBoundary,0,bStringRectPainter);
}
if (eeStringTouched){
canvas.drawRect(bRectBoundary,viewHeight,viewWidth,0,eeStringRectPainter);
}

invalidate();
}

}

最佳答案

将底部设置为 0 应该是问题所在。我用以下矩形边尝试了您的代码及其工作原理:

if (eStringTouched) {
canvas.drawRect(0, 0, 200, 200, eStringRectPainter);
}

为底部设置一个大于顶部的正值,为右侧设置一个大于左侧的正值。

关于java - Canvas.drawRect() 不绘制任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48107812/

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