How to add rectangles on top of existing rectangle in canvas

前端 未结 3 503
暗喜
暗喜 2020-12-04 01:45

I\'m trying to add some red rectangles within my existing canvas on top of specific boxes exactly like the expected result image but they don\'t appear at a

3条回答
  •  旧时难觅i
    2020-12-04 02:12

    You are drawing all of the rectangles, but it looks like you want to skip all of the "odd" rectangles - or every second rectangle... and be sure to change the color to "red" - something like this:

        //draw red windows
        for (int i = 0; i < 4; i++) {
            mWindowPaint.setStyle(Paint.Style.STROKE);//add this
            int left = i * rectWidth;
            int right = left + rectWidth;
            if (i == 1){
                mWindowPaint.setStyle(Paint.Style.FILL); // change to this
            }
    
            if (i % 2 == 0) {
                Rect rect = new Rect(left, 0, right, topRectHeight);
                canvas.drawRect(rect, mRedPaint);
                Rect rect2 = new Rect(left, h - bottomRectHeight, right, h);
                canvas.drawRect(rect2, mRedPaint);
            }
        }
    }
    

    EDIT:

    I think the "filled" rectangle on the bottom is supposed to be more like:

        //draw red windows
        for (int i = 0; i < 4; i++) {
            int left = i * rectWidth;
            int right = left + rectWidth;
    
            mWindowPaint.setStyle(Paint.Style.STROKE);//add this
            if (i % 2 == 0) {
                Rect rect = new Rect(left, 0, right, topRectHeight);
                canvas.drawRect(rect, mRedPaint);
                if (i == 1){
                    mWindowPaint.setStyle(Paint.Style.FILL); // change to this
                }
                Rect rect2 = new Rect(left, h - bottomRectHeight, right, h);
                canvas.drawRect(rect2, mRedPaint);
            }
        }
    }
    

提交回复
热议问题