Search code examples
javaandroidandroid-canvasbackground-colordrawrectangle

Android draw multiple Rect as background color


I used an canvas to draw multiple textures on it. these textures are rectangles and now I want to use these textures with parts of them invisble, so I could draw background colors behind the textures to have teh same texture with different colors without adding the same picture with different colors. I tried to add Rects like this:

for(Coordinate c : ch.getVisibleCoords()) {
    ShapeDrawable sD = new ShapeDrawable();
    Rect r = new Rect(c.getxS(), 
                      c.getyS(),
                      (sh.getScreenWidth()-c.getxS()-sh.getTSize()),
                      (sh.getScreenHeight()-c.getyS()-sh.getTSize()));
    sD.setBounds(r);
    textureColorRects.add(sD);
}

each coordinate represents an texture the xS and yS values are the positions at the screen, for example coordinate 1|1 could have xS=0 | yS=0 and 2|1 xS=48 (48=texturesize) | yS=0. I tried this with ShapeDrawable and Rectangles itself, in the first case it will draw everything the same color expect of one y-line and in the other case it will draw just some buggy shit. Is there another way to do this or may I didn't understood how to setup those rectangles, I can't figure out how that left, top, right, bottom stuff works. The rest of the code is here for you so you can see how I draw the ShapeDrawables:

int i = 0;
for(Coordinate c : ch.getVisibleCoords()) {
    ShapeDrawable sD = textureColorRects.get(i);
    Paint color = new Paint();
    color.setColor(c.getLandscape().getType().getColor());
    color.setStyle(Paint.Style.FILL);
    sD.getPaint().set(color);
    sD.draw(canvas);
}

The textureColorRects is a list containing all ShapeDrawables.

Thank you very much for reading.


Solution

  • I found an solution it's a problem other people had too (was just hard to find) it's a bit hard to understand how the Rect works the values for left, top, right and bottom are seen like the beginning and the ed point for example I want a rectangle of the size 16*16 and at the point x=5|y=18 on the screen, so I need to set the right value to x+size (5+16) and the bottom to y+size (18+16). The lft and top can be set to the left upper edge of the rect (start position).