我正在嘗試爲Android創建自定義視圖。我需要一個可以容納另外7個矩形的大矩形(距主矩形內部等間距和填充,代表星期幾)。隨着我當前的代碼,我得到以下結果:在矩形內繪製等距矩形
但是我期待的應該是(比例並不重要,只要空間是相同的):
這是我的代碼。任何幫助和建議將appriciated!
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Main rectangle
Rect boxMain = new Rect();
boxMain.set(getLeft() + 25, getTop() + 25, getRight() - 25, getBottom() - 25);
int hMiniBox = boxMain.height()/7; //height for each of 7 rectangles
int space = 10; //Space between each rectangle inside the main rectangle
int rectH = hMiniBox; //Height of each rectangle
//Draw the main rectangle
canvas.drawRect(boxMain, _paintProgressBoxBorder);
//Draw 7 rectangles inside main rectangle
for(int i = 0; i <7; i++)
{
Rect rect = new Rect();
rect.set(
boxMain.left + space,
boxMain.top + space,
boxMain.right - space,
rectH
);
canvas.drawRect(rect, _paintProgressMiniBoxesBorder);
rectH += hMiniBox;
}
invalidate();
}
你不動的長方形的頂部邊角下來。你只是使每個連續的矩形更高。 – mbeckish