View.getLocationInWindow(int[] location)

一个控件在其父窗口中的坐标位置

View.getLocationOnScreen(int[] location)

一个控件在其整个屏幕上的坐标位置

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

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

 

最新文章

  1. 第六章 大数据,6.3 突破传统,4k大屏的沉浸式体验(作者: 彦川、小丛)
  2. Performing a thread dump in Linux or Windows--reference
  3. oracle分配角色和表空间
  4. Oracle基础学习5-- Oracle权限之”角色”
  5. Javascript事件模型(一):DOM0事件和DOM2事件
  6. 彻底弄懂CommonJS和AMD/CMD!
  7. Python基础数据类型之列表和元组
  8. spring的DI.IoC是什么
  9. BZOJ 1491: [NOI2007]社交网络(Floyd+暴力乱搞)
  10. Leetcode: Sliding Window Median
  11. Matlab内嵌图像
  12. 【4】数独(Sudoku Killer)(深度优先遍历)
  13. laravel安装laravel-ide-helper扩展进行代码提示(二)
  14. Python打包—Pyinstaller
  15. java实现计算器
  16. 在 Wiki 标记中添加无序列表
  17. MySQL事务(三)
  18. 文件系统(File System)
  19. avaScript 的基础学习(一)
  20. css3记事

热门文章

  1. Poj 4227 反正切函数的应用
  2. binder对于boolean类型的传递
  3. Pro Tools安装图文教程
  4. poj1062昂贵的聘礼
  5. Android API Guides---RenderScript
  6. python测试网页是否能正常登陆
  7. PHP之十六个魔术方法详细介绍
  8. docker教程之从一头雾水到不一头雾水(2)
  9. Java 入门课程视频实战-0基础 上线了,猜拳游戏,ATM实战,欢迎围观
  10. ROS多个网段做隔离