我的Android進階之旅------>android中getLocationInWindow 和 getLocationOnScreen的差別

來源:互聯網
上載者:User

標籤:null   app   gb2312   div   one   ora   and   soft   new   

View .getLocationInWindow(int[] location)

一個控制項在其父表單中的座標位置

View.getLocationOnScreen(int[] location)

一個控制項在其整個螢幕上的座標位置



watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">

getLocationInWindow是以B為原點的C的座標

getLocationOnScreen以A為原點。


以下是getLocationOnScreen示範範例

start = (Button) findViewById(R.id.start);int []location=new int[2];start.getLocationOnScreen(location);int x=location[0];//擷取當前位置的橫座標int y=location[1];//擷取當前位置的縱座標


以下是getLocationInWindow示範範例

start = (Button) findViewById(R.id.start);int []location=new int[2];start.getLocationInWindow(location);int x=location[0];//擷取當前位置的橫座標int y=location[1];//擷取當前位置的縱座標

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

 附上源碼

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

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;        }    }

 



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

  歐陽鵬  歡迎轉載,與人分享是進步的源泉!

  轉載請保留原文地址:http://blog.csdn.net/ouyang_peng

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

 



 

我的Android進階之旅------&gt;android中getLocationInWindow 和 getLocationOnScreen的差別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.