Android gets the coordinates of the control in the screen

Source: Internet
Author: User

Getlocationonscreen , calculates the x, Y value of the view in the global coordinate system (note that this value is calculated from the top of the screen, i.e. the height of the notification bar is included)//Gets the absolute coordinates in the current screen

Getlocationinwindow , calculates the x, Y value of the view in its widnow coordinates,//Gets the absolute coordinates within the entire window (not very understanding = =,)

getLeft , getTop, getbottom,getRight, this group is getting the coordinates relative to its father

If the OnCreate () event of the activity outputs those parameters, it is all 0, so that the UI controls are loaded before they can be obtained.

[Java]View PlainCopy
  1. Package xiaosi.location;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.view.View;
  5. Import Android.view.View.OnClickListener;
  6. Import Android.widget.Button;
  7. Import Android.widget.ImageView;
  8. Public class Locationactivity extends Activity {
  9. /** Called when the activity is first created. * /
  10. private ImageView t = null;
  11. Private button button = null;
  12. @Override
  13. public void OnCreate (Bundle savedinstancestate) {
  14. super.oncreate (savedinstancestate);
  15. Setcontentview (R.layout.main);
  16. t = (ImageView) Findviewbyid (R.ID.L);
  17. Button = (button) Findviewbyid (R.id.button);
  18. Button.setonclicklistener (new Buttonlistener ());
  19. }
  20. public class Buttonlistener implements onclicklistener{
  21. public void OnClick (View v)
  22. {
  23. int[] location = new int[2];
  24. T.getlocationonscreen (location);
  25. int x = location[0];
  26. int y = location[1];
  27. System.out.println ("x:" +x+"y:" +y);
  28. System.out.println ("picture Corners Left:" +t.getleft () +"right:" +t.getright () +"Top:" +t.gettop () +"Bottom:" +  T.getbottom ());
  29. }
  30. }
  31. }


[Java]View PlainCopy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  3. Android:layout_width="Fill_parent"
  4. android:layout_height="Fill_parent"
  5. android:orientation="vertical" >
  6. <button
  7. android:id="@+id/button"
  8. Android:layout_width="Fill_parent"
  9. android:layout_height="Wrap_content"
  10. android:text="button"/>
  11. <imageview
  12. android:id="@+id/l"
  13. Android:layout_width="Wrap_content"
  14. android:layout_height="Wrap_content"
  15. android:layout_gravity="center"
  16. android:src="@drawable/a"/>
  17. </LinearLayout>


Android gets the coordinates of the control in the screen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.