1. Relative Position:
GetLeft (), GetRight (), GetTop (), Getbottom ()
In Android, you can put left equivalent to the x-axis value, top equivalent to the y-axis value, through the two values of the Android system can know the view of the drawing starting point, in the Wdith and Height can get the view up and down the specific value, you can on the screen absolute position drawing view. Right and bottom are calculated as follows:
right = left + width;
Bottom = top + height;
View left position View.getleft ()
View right position view.getright ()
The top position of the view view.gettop ();
View bottom position view.getbottom ();
View width view.getwidth ();
View Height view.getheight ()
Wrong understanding, the location of the green area is for the blue area of the (0, 0) coordinates of the value, from the lower right corner of the printed coordinate values can be seen with the below I listed values inconsistent,
Just look at the picture below.
Blue Area Position left = 0, top = 0 coordinates (0, 0)
Yellow area Position left =, top = 115 coordinates (60, 115)
Green Zone Position left = 170 coordinates (115, 170)
Summary: The left, top, right, bottom values of the view are relative to their parent view, and the green area is for the coordinates of its parent view (that is, the yellow area is (0, 0) points) and should not be (115, 170) but (55, 55)
2. Absolute position:
- Getlocalvisiblerect
- Getglobalvisiblerect
- Getlocationonscreen
- Getlocationinwindow
1) Getlocationinwindow
int[] Position = new INT[2];
Textview.getlocationinwindow (position);
System.out.println ("Getlocationinwindow:" + position[0] + "," + position[1]);
This method stores the upper-left coordinate of the view in an array. This coordinate is relative to the current activity.
For normal activity, the y-coordinate is the visible status bar height + visible title bar height +view the upper-left corner to the bottom of the title bar.
In the case of hidden status bar/title bar, their height is calculated at 0.
For dialog-style activity, the y-coordinate is the distance from the visible title bar height +view to the bottom of the title bar.
At this point there is no disregard for the status bar.
2) Getlocationonscreen
int[] Position = new INT[2];
Textview.getlocationonscreen (position);
System.out.println ("Getlocationonscreen:" + position[0] + "," + position[1]);
This method is similar to the above, and it also stores the upper-left corner of the view in the array. But this coordinate is relative to the entire screen.
The y-coordinate is the distance from the upper-left corner of the view to the top of the screen.
3) Getglobalvisiblerect
Rect viewrect = new rect ();
Textview.getglobalvisiblerect (Viewrect);
System.out.println (Viewrect);
The method is to construct a rect to "set" the view. The coordinates of this rect are relative to the current activity.
For normal activity, the top of the rect is the visible status bar height + the distance from the upper left corner to the bottom of the title bar of the visible title bar height +rect.
For dialog-style activity, the y-coordinate is the top of the rect that is visible, the distance from the upper-left corner to the bottom of the title bar for the +rect height of the title bar.
At this point there is no disregard for the status bar.
4) Getlocalvisiblerect
Rect globerect = new rect ();
Button.getlocalvisiblerect (Globerect);
Attention:
The above method is called in the OnCreate method, which returns 0 because the view is not loaded.
It is recommended to obtain in the Onwindowfocuschanged method, in some cases onwindowfocuschanged when it is not good (such as Activitygroup), you can write:
Mtextview.post (New Runnable () {
@Override
public void Run () {
Rect viewrect = new rect ();
Mtextview.getlocalvisiblerect (Viewrect);
Mtreescrollview.setrect (Viewrect);
}
});
This will perform the method of acquiring the location after the view has finished loading.
Where to get view in Android app