我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的区别
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的区别的更多相关文章
- 我的Android进阶之旅------>Android中查看应用签名信息
一.查看自己的证书签名信息 如上一篇文章<我的Android进阶之旅------>Android中制作和查看自定义的Debug版本Android签名证书>地址:http://blog ...
- 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色
通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...
- 我的Android进阶之旅------> Android在TextView中显示图片方法
面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用Tex ...
- 我的Android进阶之旅------>Android中AsyncTask源码分析
在我的<我的Android进阶之旅------>android异步加载图片显示,并且对图片进行缓存实例>文章中,先后使用了Handler和AsyncTask两种方式实现异步任务机制. ...
- 我的Android进阶之旅------> Android为TextView组件中显示的文本加入背景色
通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...
- 我的Android进阶之旅------>Android利用温度传感器实现带动画效果的电子温度计
要想实现带动画效果的电子温度计,需要以下几个知识点: 1.温度传感器相关知识. 2.ScaleAnimation动画相关知识,来进行水印刻度的缩放效果. 3.android:layout_weight ...
- 我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(三)Android客户端功能实现
我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端(地址:http://blog.csdn.net/ouyang_pen ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之实现游戏逻辑(五)
在上一篇<我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)>中提到的两个类: GameConf:负责管理游戏的 ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)
正如在<我的Android进阶之旅------>Android疯狂连连看游戏的实现之状态数据模型(三)>一文中看到的,在AbstractBoard的代码中,当程序需要创建N个Piec ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之状态数据模型(三)
对于游戏玩家而言,游戏界面上看到的"元素"千变万化:但是对于游戏开发者而言,游戏界面上的元素在底层都是一些数据,不同数据所绘制的图片有所差异而已.因此建立游戏的状态数据模型是实现游 ...
随机推荐
- 转:svn 更新指定文件夹
通常由于创建很多个branch和tag,当我们要去checkout指定tag和branch的时候,会不得不把整个branch/tag目录checkout出来.是不是有点傻??!!! 那么如何有选择ch ...
- PHP使用mysqli连接MySQL数据库
使用mysqli函数库连接MySQL,支持面向对象和面向过程两种方式: 1.面向对象的使用方式 建立一个连接 $db = new mysqli('localhost', 'root', '123456 ...
- 【VBA】切换引用样式
在Excle中有两种引用方式,例如:第一行第一列的单元格可以是:A1 也可以是R1C1 切换引用样式的代码如下: Sub 切换引用样式() Application.ReferenceStyle = ...
- MATLAB 2014a 在Mac os x yosemite 10.10 Retina显示模糊的解决的方法
恐怕非常多童鞋在升级了yosemite之后都遇到了Matlab的问题. 之前转载的一篇文章写了安装的方法,本文说一下解决Retina屏显示模糊的办法. 那么因为Matlab 2014a使用自带的jav ...
- ios7中的edgesForExtendedLayout
edgesForExtendedLayout是一个类型为UIExtendedEdge的属性,指定边缘要延伸的方向. 因为iOS7鼓励全屏布局,所以它的默认值是UIRectEdgeAll——四周边缘都延 ...
- ajax读取文件内容
读取json文件 $.ajax({ url: 'manifest.webapp', type: 'GET', dataType: 'json',//类型不对会出错 timeout: 1000, //设 ...
- html禁止图片拖拽移动在新窗口打开
一直觉得直接从网站的表格上复制数据挺方便的, 今天,领导突然说网站上的图片可以被别人拖走了,必须禁止,哎,果然只有领导才考虑得到这种事情啊 so, 将ondragstart="return ...
- python测试网页是否能正常登陆
#!/usr/bin/python #encoding:utf-8 ##实现网页的登陆检查 import HTMLParser import urlparse import cookielib imp ...
- redis 的安装与启动
1.redis介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis 是一个高性能的key-val ...
- Linux Suse 查看wwn号码的方法
查看wwn号码 cat /sys/class/fc_host/host*/port_name *代表全部host目录