Search code examples
androidrect

In Android, how do I give a Rect an X and Y position?


I'm trying to draw a simple Rect on a Canvas that is at the X coordinate of 360, and the Y coordinate of 0. I can draw my Rect if I make the X coordinate to 0, but when I make it 360, the square becomes distorted and becomes a rectangular shape rather than a square anymore. My screen size is 640px wide, so there should be no problem here. I can draw Bitmaps with the same specifications and it will draw normally. Why is it that Rects don't draw correctly? Is it somehow that the X coordinate is only in DP rather than PX? Then why does that affect the actual size of the Rect? I'm really confused.

            Rect square6 = new Rect();
            square6.set(360, 0, 60, 60);

Solution

  • You should read the reference to the Rect in Android, The set func of Rect is public void set (int left, int top, int right, int bottom), you set your rect start from (360, 0) and ends at (60, 60), you should change the parmas to (360, 0, 420, 60). It will work.