android中getLocationInWindow和getLocationOnScreen的区别

  • View.getLocationInWindow(int[] location)
    一个控件在其父窗口中的坐标位置
  • View.getLocationOnScreen(int[] location)
    一个控件在其整个屏幕上的坐标位置
    多个控件嵌套

  • getLocationInWindow是以B为原点的C的坐标

  • getLocationOnScreen以A为原点。
start = (Button) findViewById(R.id.start);  
        int []location=new int[2];  
        start.getLocationOnScreen(location);  
        int x=location[0];//获取当前位置的横坐标  
        int y=location[1];//获取当前位置的纵坐标  
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);  
    }  
/** 
   * <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;  
      }  
  }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值