Canvas.drawRect()没有绘制任何东西

时间:2018-01-05 05:41:52

标签: java android canvas android-canvas

我的画布类中的画家不是绘制矩形,尽管我将其配置为这样做。

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

如下面的屏幕截图所示(从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();
}

}

1 个答案:

答案 0 :(得分:2)

将底部设为0应该是问题所在。我尝试使用下面的rect side及其工作代码:

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

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