Abhishek Choudhary
Abhishek Choudhary

Reputation: 8395

how to draw a rectangle in specific postion in android

I am trying to draw rectangle inside a Rectangle. I am expecting the child to draw in lower side of parent rectangle but opposite is happening. Actually i found there that, inside canvas.drawRect there is no specific position to decide where to draw. May be I am missing something because of new in android. This is the api I am using-

canvas.drawRect(x+(x/3)+rM.x(), y/9, w-rM.width(), h/5, paint);

Following the getting and expected screenshot

enter image description here

Following are the coordinates I am getting -

For bigger rectangle - 
x      - 234
y      - 89
width  - 593
height - 627


For smaller Rectangle
rm.x      - 39
rm.y      - 8
rm.width  - 49
rm.height - 30

I used the formula-

canvas.drawRect(x+(x/3)+rM.x(), y/9, w-rM.width(), h/5, paint);

Upvotes: 1

Views: 6726

Answers (1)

Akash Raghav
Akash Raghav

Reputation: 1153

For the future viewers -

you can create rectangle object and then pass it to drawRect() method. Example -

Rect rect = new Rect();
rect.left = x;
rect.top = y;
rect.right = x + width;
rect.bottom = y + height;

canvas.drawRect(rect, paint);

Upvotes: 6

Related Questions