My Android advanced tour ------ & gt; Difference between getLocationInWindow and getLocationOnScreen in android, getlocationonscreen

Source: Internet
Author: User

My Android advanced tour ------> difference between getLocationInWindow and getLocationOnScreen in android, getlocationonscreen
View. GetLocationInWindow (int [] location)

Coordinates of a widget in its parent window

View. getLocationOnScreen (int [] location)

Coordinates of a widget on its entire Screen




GetLocationInWindow is the coordinate of C with B as the origin.

GetLocationOnScreen uses A as the origin.


The following is an example of getLocationOnScreen.

Start = (Button) findViewById (R. id. start); int [] location = new int [2]; start. getLocationOnScreen (location); int x = location [0]; // obtain the abscissa int y = location [1] of the current position; // obtain the ordinate of the current position


The following is an example of getLocationInWindow.

Start = (Button) findViewById (R. id. start); int [] location = new int [2]; start. getLocationInWindow (location); int x = location [0]; // obtain the abscissa int y = location [1] of the current position; // obtain the ordinate of the current position

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

Add source code

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

View . GetLocationInWindow (int [] location)

/**     * <p>Computes the coordinates of this view in its window. The argument     * must be an array of two integers. After the method returns, the array     * contains the x and y location in that order.</p>     *     * @param location an array of two integers in which to hold the coordinates     */    public void getLocationInWindow(int[] location) {        if (location == null || location.length < 2) {            throw new IllegalArgumentException("location must be an array of two integers");        }        if (mAttachInfo == null) {            // When the view is not attached to a window, this method does not make sense            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 on the screen. The argument     * must be an array of two integers. After the method returns, the array     * contains the x and y location in that order.</p>     *     * @param location an array of two integers in which to hold the coordinates     */    public void getLocationOnScreen(int[] location) {        getLocationInWindow(location);        final AttachInfo info = mAttachInfo;        if (info != null) {            location[0] += info.mWindowLeft;            location[1] += info.mWindowTop;        }    }

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

Author: Ouyang Peng: Welcome to repost. sharing with others is the source of progress!

Reprinted Please retain the original address: http://blog.csdn.net/ouyang_peng

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



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.