Reputation: 101
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<>();
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,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 < listRect.size(); i++){
canvas.drawRect(listRect.get(i), listColor.get(i));
}
}
Upvotes: 0
Views: 48
Reputation: 2703
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);
Upvotes: 2