My Android Advanced tour------The difference between Getlocationinwindow and Getlocationonscreen in >android

Source: Internet
Author: User

View . Getlocationinwindow (int[] location)

The coordinate position of a control in its parent form

View. Getlocationonscreen (int[] location)

The coordinate position of a control on its entire screen



Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

Getlocationinwindow is the coordinate of C, which is the origin of B.

Getlocationonscreen A as the Origin point.


The following is a sample Getlocationonscreen demo

Start = (Button) Findviewbyid (r.id.start); int []location=new int[2];start.getlocationonscreen (location); int x= location[0];//gets the horizontal axis of the current position int y=location[1];//Gets the ordinate of the current position


Below are Getlocationinwindow Demo sample

Start = (Button) Findviewbyid (r.id.start); int []location=new Int[2];start.getlocationinwindow (location); int x= location[0];//gets the horizontal axis of the current position int y=location[1];//Gets the ordinate of the current position

==================================================================================================

Attach source code

==================================================================================================

View . Getlocationinwindow (int[] location)

/** * <p>computes The coordinates of this view in its window. The argument * must is an array of both integers. After the method returns, the array * contains the X and y location in that order.</p> * * @param locati        On a array of integers in which to hold the coordinates */public void Getlocationinwindow (int[] location) {  if (location = = NULL | | Location.length < 2) {throw new IllegalArgumentException ("Location must is an        Array of integers "); if (Mattachinfo = = null) {//When the view isn't attached to a window, this method does not make Sen            Se location[0] = location[1] = 0;        Return        } float[] position = mattachinfo.mtmptransformlocation;        Position[0] = position[1] = 0.0f;        if (!hasidentitymatrix ()) {Getmatrix (). mappoints (position);        } position[0] + = Mleft;        POSITION[1] + = Mtop; Viewparent viewparent = MPaRent            while (viewparent instanceof view) {Final View view = (view) viewparent;            Position[0]-= VIEW.MSCROLLX;            POSITION[1]-= view.mscrolly;            if (!view.hasidentitymatrix ()) {View.getmatrix (). mappoints (position);            } position[0] + = View.mleft;            POSITION[1] + = View.mtop;         Viewparent = view.mparent;  } if (viewparent instanceof Viewrootimpl) {//*cough* final Viewrootimpl VR = (Viewrootimpl)            Viewparent;        POSITION[1]-= vr.mcurscrolly;        } location[0] = (int) (position[0] + 0.5f);    LOCATION[1] = (int) (position[1] + 0.5f); }
View. Getlocationonscreen (int[] location)

  /** * <p>computes The coordinates of this view in the screen     . The argument     * must is an array of both integers. After the method returns, the array     * contains the x and y location in that order.</p>     *     * @param locati On a array of integers in which to hold the coordinates     *    /public void Getlocationonscreen (int[] location) { C8/>getlocationinwindow (location);        Final Attachinfo info = mattachinfo;        if (info! = null) {            location[0] + = Info.mwindowleft;            LOCATION[1] + = Info.mwindowtop;        }    }



====================================================================================

Ouyangpeng welcome reprint, sharing with people is the source of progress!

Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng

====================================================================================



My Android Advanced tour------The difference between Getlocationinwindow and Getlocationonscreen in &gt;android

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.