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 img = null;private Button button = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); img = (ImageView)findViewById(R.id.image); 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]; img.getLocationOnScreen(location); int x = location[0]; int y = location[1]; System.out.println(x:+x+y:+y); System.out.println(Left:+img.getLeft());System.out.println(Right:+img.getRight());System.out.println(Top:+img.getTop());System.out.println(Bottom:+img.getBottom());} }}
GetLocationOnScreenCalculate the x and y values of the view in the global coordinate system. (Note that the 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, Calculate the x and y coordinates of the view's widnow value, // obtain the absolute coordinates in the entire window (not very understandable = ,)
GetLeft,GetTop,GetBottom,GetRight,This group is used to obtain coordinates relative to its father.
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.