Draw rectangles with space between them in Android
I want to开发者_JS百科 draw five rectangle bars in Android. I have the regtangles, but now I want them to be a bit spaced apart.
I want them to be aligned at the bottom, and with the same distance between them.
for (int i= 0; i<4; i++) {
int ce = heigth[i];
Paint rectanglePaint = new Paint();
rectanglePaint.setARGB(255, 0, 0, 0);
rectanglePaint.setStrokeWidth(2);
rectanglePaint.setColor(Color.BLUE);
rectanglePaint.setStyle(Style.STROKE);
Rect rectangle = new Rect(35+10*ce, 150, 10, 10*ce); //in pixels
//rectangle.offset(50, 50);
rectangle.offsetTo(55+10*ce, 150);
//canvas.translate(10, 0);
canvas.drawRect(rectangle, rectanglePaint);
I have tried with offset, offsetTo, translate, but can't find the logic in using them. I want them all to start at different spots, like they move 35 dip to the right and are all 30 dip wide. Although I add 35 to the left, they still originate from the same spot.
Perhaps height[i]
do not change?
Thisn should create four 10x10 rectangles separated 35 px to the left each other. BTW, you do not need to create four Paint objects. Reuse the same for the four rectangles for improved efficiency.
Paint rectanglePaint = new Paint();
rectanglePaint.setARGB(255, 0, 0, 0);
rectanglePaint.setStrokeWidth(2);
rectanglePaint.setColor(Color.BLUE);
rectanglePaint.setStyle(Style.STROKE);
for (int i= 0; i<4; i++) {
Rect rectangle = new Rect(35*i, 150, 35*i+10, 160);
canvas.drawRect(rectangle, rectanglePaint);
}
精彩评论