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

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

 

我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的区别的更多相关文章

  1. 我的Android进阶之旅------>Android中查看应用签名信息

    一.查看自己的证书签名信息 如上一篇文章<我的Android进阶之旅------>Android中制作和查看自定义的Debug版本Android签名证书>地址:http://blog ...

  2. 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  3. 我的Android进阶之旅------> Android在TextView中显示图片方法

    面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用Tex ...

  4. 我的Android进阶之旅------>Android中AsyncTask源码分析

    在我的<我的Android进阶之旅------>android异步加载图片显示,并且对图片进行缓存实例>文章中,先后使用了Handler和AsyncTask两种方式实现异步任务机制. ...

  5. 我的Android进阶之旅------> Android为TextView组件中显示的文本加入背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  6. 我的Android进阶之旅------>Android利用温度传感器实现带动画效果的电子温度计

    要想实现带动画效果的电子温度计,需要以下几个知识点: 1.温度传感器相关知识. 2.ScaleAnimation动画相关知识,来进行水印刻度的缩放效果. 3.android:layout_weight ...

  7. 我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(三)Android客户端功能实现

    我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端(地址:http://blog.csdn.net/ouyang_pen ...

  8. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之实现游戏逻辑(五)

    在上一篇<我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)>中提到的两个类: GameConf:负责管理游戏的 ...

  9. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)

    正如在<我的Android进阶之旅------>Android疯狂连连看游戏的实现之状态数据模型(三)>一文中看到的,在AbstractBoard的代码中,当程序需要创建N个Piec ...

  10. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之状态数据模型(三)

    对于游戏玩家而言,游戏界面上看到的"元素"千变万化:但是对于游戏开发者而言,游戏界面上的元素在底层都是一些数据,不同数据所绘制的图片有所差异而已.因此建立游戏的状态数据模型是实现游 ...

随机推荐

  1. Java过滤器(Filter)与SpringMVC拦截器(Interceptor)之间的关系与区别

    过滤器和拦截器的区别: ①拦截器是基于java的反射机制的,而过滤器是基于函数回调. ②拦截器不依赖与servlet容器,过滤器依赖与servlet容器. ③拦截器只能对action请求起作用,而过滤 ...

  2. Word文档打不开怎么办

    目前一些主流的办公软件给大家日常工作带来了很大便利,比如:Microsoft Office或金山WPS!我们在愉快地使用它们的同时,多少也遇到了一些让人尴尬或头疼的问题,比如:精心制作的文档,突然打不 ...

  3. ubuntu12.04 下安装nodejs

    liunx里面安装nodejs我也找了非常多文章,貌似对非常多liunx新手来讲不是非常清楚,以下是我结合一些文章,亲自实践得到的安装步骤.同一时候还有大家关心的与seajs紧密相关的spm模块的安装 ...

  4. Python列表切成多个/生成多个空列表

    li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18] #arr是被分割的list,n是每个chunk中含n元素. def chunks(arr, n) ...

  5. 怎样把报表放到网页中显示(Web页面与报表简单集成样例)

    1.问题描写叙述 如今用户开发的系统基本上趋向于BS架构的浏览器/server模式.这些系统可能由不同的语言开发.如HTML.ASP.JSP.PHP等.因此须要将制作好的报表嵌入到这些页面中. Fin ...

  6. html 上下左右都居中

    给要居中的图片或者链接所在的div 设置例如以下属性 width: px; height: wd=px&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqI ...

  7. 初探J2EE

    还记得在技术交流会上八期给我们讲的J2EE,当时就是云里来屋里去.留在自己脑子中的仅仅有两个字"规范",其他的真是一无全部. 可是如今学了后,又在脑子里留下了两个字"规范 ...

  8. CentOS7网络桥接模式下配置-经典完备

    原文地址:http://blog.csdn.net/youzhouliu/article/details/51175364 首先要将Vmware设置为桥接模式: 并选择宿主机连接的网路进行桥接: Ce ...

  9. android tooggle button

    1 http://stackoverflow.com/questions/9938315/toggle-button-in-iphone-style 2 http://blog.csdn.net/bi ...

  10. 【HBase基础教程】1、HBase之单机模式与伪分布式模式安装(转)

    在这篇blog中,我们将介绍Hbase的单机模式安装与伪分布式的安装方式,以及通过浏览器查看Hbase的用户界面.搭建hbase伪分布式环境的前提是我们已经搭建好了hadoop完全分布式环境,搭建ha ...