Android gets the coordinates of the control in the screen

Source: Internet
Author: User

 

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

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.