我的画布没有绘制我尝试绘制的所有矩形。

huangapple go评论76阅读模式
英文:

My canvas is not drawing all the rectangles im trying to draw

问题

I've checked similar questions here but the answers are doing what I'm doing already. I've tried most of the things I can think of and just can't solve it.

我已经查看了类似的问题,但答案正在做我已经在做的事情。我已经尝试了我能想到的大部分方法,但就是解决不了。

I created my own view where I draw rectangles 100x100 in size next to each other. The first rectangle is drawn correctly but the second is drawn in the color of the third rectangle, and the third rectangle is not drawn at all. I just can't figure out what I'm doing wrong.

我创建了自己的视图,在这个视图中,我绘制了相邻大小为100x100的矩形。第一个矩形绘制正确,但第二个矩形绘制成了第三个矩形的颜色,而第三个矩形根本没有绘制出来。我就是搞不清楚我做错了什么。

private void init(@Nullable AttributeSet set){
    listRect = new ArrayList<>();
    listColor = new ArrayList<>();
    loadRect();
    invalidate();
}

private void loadRect(){
    Rect rect = new Rect();
    Paint paint = new Paint();

    paint.setColor(Color.GREEN);
    rect.set(0,0,100,100);
    listColor.add(paint);
    listRect.add(rect);

    rect = new Rect();
    paint = new Paint();
    paint.setColor(Color.BLUE);
    rect.set(100,0,200,100);
    listColor.add(paint);
    listRect.add(rect);

    rect = new Rect();
    paint = new Paint();
    paint.setColor(Color.RED);
    rect.set(200,0,300,100);
    listColor.add(paint);
    listRect.add(rect);
}

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

    for(int i = 0; i < listRect.size(); i++){
        canvas.drawRect(listRect.get(i), listColor.get(i));
    }
}

我的画布没有绘制我尝试绘制的所有矩形。

英文:

Ive checked similar questions here but the answers are doing what im doing already. Ive tried most of the things i can think of and just cant solve it.

I created my own view where i draw rectangles 100x100 in size next to eachother. The first rectangle is drawn correctly but the second is drawn in the color of the third rectangle and the third rectangle is not drawn at all. I just cant figure out what im doing wrong.

private void init(@Nullable AttributeSet set){
        listRect = new ArrayList&lt;&gt;();
        listColor = new ArrayList&lt;&gt;();
        loadRect();
        invalidate();
    }

    private void loadRect(){
        Rect rect = new Rect();
        Paint paint = new Paint();

        paint.setColor(Color.GREEN);
        rect.set(0,0,100,100);
        listColor.add(paint);
        listRect.add(rect);


        rect = new Rect();
        paint = new Paint();
        paint.setColor(Color.BLUE);
        rect.set(100,0,100,100);
        listColor.add(paint);
        listRect.add(rect);

        rect = new Rect();
        paint = new Paint();
        paint.setColor(Color.RED);
        rect.set(200,0,100,100);
        listColor.add(paint);
        listRect.add(rect);
    }

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

        for(int i = 0; i &lt; listRect.size(); i++){
            canvas.drawRect(listRect.get(i), listColor.get(i));
        }
    }

我的画布没有绘制我尝试绘制的所有矩形。

答案1

得分: 2

The rect 构造函数接受4个参数:

public Rect (int left, 
                int top, 
                int right, 
                int bottom)

set 方法也是一样的:

public void set (int left, 
                int top, 
                int right, 
                int bottom)

所以你最后一个矩形是不正确的(我指的是 right):

rect.set(200,0,100,100);

应该是:

rect.set(200,0,300,100);
英文:

The rect constructor takes 4 arguments:

public Rect (int left, 
                int top, 
                int right, 
                int bottom)

Same for the set method:

public void set (int left, 
                int top, 
                int right, 
                int bottom)

So your last rectangle is not correct (I mean right):

rect.set(200,0,100,100);

It should be:

rect.set(200,0,300,100);

huangapple
  • 本文由 发表于 2020年8月5日 03:52:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63254169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定