GetLocationOnScreen: calculates the x and y values of the view in the global coordinate system. (Note that this value is counted from the top of the screen, that is, the height of the notification bar is included) // obtain the absolute coordinates on the current screen
GetLocationInWindow: calculates the x and y coordinates of the view's widnow, and // obtains the absolute coordinates of the view within the window (not very understandable = ,)
GetLeft, getTop, getBottom, and getRight are the coordinates of the parent group.
If the parameters output in the OnCreate () event of the Activity are all 0, these parameters can be obtained only after the UI control is fully loaded.
[Java] package xiaosi. location;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. ImageView;
Public class LocationActivity extends Activity {
/** Called when the activity is first created .*/
Private ImageView t = null;
Private Button button = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
T = (ImageView) findViewById (R. id. l );
Button = (Button) findViewById (R. id. button );
Button. setOnClickListener (new buttonListener ());
}
Public class buttonListener implements OnClickListener {
Public void onClick (View v)
{
Int [] location = new int [2];
T. getLocationOnScreen (location );
Int x = location [0];
Int y = location [1];
System. out. println ("x:" + x + "y:" + y );
System. out. println ("Left corner of the image:" + t. getLeft () + "Right:" + t. getRight () + "Top:" + t. getTop () + "Bottom:" + t. getBottom ());
}
}
}
Package xiaosi. location;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. ImageView;
Public class LocationActivity extends Activity {
/** Called when the activity is first created .*/
Private ImageView t = null;
Private Button button = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
T = (ImageView) findViewById (R. id. l );
Button = (Button) findViewById (R. id. button );
Button. setOnClickListener (new buttonListener ());
}
Public class buttonListener implements OnClickListener {
Public void onClick (View v)
{
Int [] location = new int [2];
T. getLocationOnScreen (location );
Int x = location [0];
Int y = location [1];
System. out. println ("x:" + x + "y:" + y );
System. out. println ("Left corner of the image:" + t. getLeft () + "Right:" + t. getRight () + "Top:" + t. getTop () + "Bottom:" + t. getBottom ());
}
}
}
[Java] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<Button
Android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "button"/>
<ImageView
Android: id = "@ + id/l"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: src = "@ drawable/a"/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<Button
Android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "button"/>
<ImageView
Android: id = "@ + id/l"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: src = "@ drawable/a"/>
</LinearLayout>
From sunset hut