两个相邻的矩形不起作用 (Java)

Two rectangles next to eachother is not working (Java)

我正在尝试画一条线,但我总是遇到问题。我想实现这样的目标:

private Paint red = new Paint();
private Paint orange = new Paint();

red.setColor(Color.parseColor("#FF0000"));
orange.setColor(Color.parseColor("#FF8C00"));

canvas.drawRect(0, 400, 300, 0, red);
canvas.drawRect(300, 400, 300, 0, orange);

橙色条与红色条位于同一位置...为什么?

再看看文档:Canvas.drawRect

drawRect(float left, float top, float right, float bottom, Paint paint)
Draw the specified Rect using the specified paint.

所以你的最后两个坐标值(rightbottom)不是长度而是位置。

你能看到你的代码中橙色矩形 (300-300) 的长度为 0 吗?这就是为什么你看不到它。所以试试这个:

    canvas.drawRect(0, 400, 300, 0, red);
    canvas.drawRect(300, 400, 600, 0, orange);

你有400的顶部和0的底部。很奇怪。你可能想交换它们。但是,问题是您的第一行的左边界为 0,右边界为 300,而第二行是橙色点,左边界为 300,右边界恰好在 300。