我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的差别
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进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的差别的更多相关文章
- 我的Android进阶之旅------>关于android:layout_weight属性的详细解析
		
关于androidlayout_weight属性的详细解析 效果一 效果二 图3的布局代码 图4的布局代码 效果三 图7代码 图8代码 效果四 效果五 版权声明:本文为[欧阳鹏]原创文章,欢迎转载,转 ...
 - 我的Android进阶之旅------>关于android:layout_weight属性的一个面试题
		
最近碰到一个面试题,按照下图,由Button和EditText组成的界面下厨布局代码,解决这题目需要使用android:layout_weight的知识. 首先分析上图所示的界面可以看成一下3个部分. ...
 - 我的Android进阶之旅------> Android在TextView中显示图片方法
		
面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包括图像的文本信息).并简要说明实现方法. 答案:Android SDK支持例如以下显示富文本信息的方式. 1.使用T ...
 - 我的Android进阶之旅------>Android字符串资源中的单引號问题error: Apostrophe not preceded by 的解决的方法
		
刚刚在string字符串资源文件里,写了一个单引號.报错了,错误代码例如以下 error: Apostrophe not preceded by \ (in OuyangPeng's blog ) 资 ...
 - 我的Android进阶之旅------> Android为TextView组件中显示的文本加入背景色
		
通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...
 - 我的Android进阶之旅------>Android系统设置默认来电铃声、闹钟铃声、通知铃声
		
首先了解Android系统本身提供的默认铃声文件,这些文件都放在 /system/media/audio 文件夹下. /system/media/audio/ringtones 系统来电铃声 ...
 - 【我的Android进阶之旅】Android 混淆文件资源分类整理
		
之前将所有的混淆都配置在一个 proguard-rules.pro 这个Android Studio新建项目时自动生成的文件里面,而随着项目功能迭代越来越多,代码量越来越多,引用的第二方库.第三方库都 ...
 - 我的Android进阶之旅------>Android实现音乐示波器、均衡器、重低音和音场功能
		
本实例来自于<疯狂Android讲义>.要实现详细的功能,须要了解下面API: MediaPlayer 媒体播放器 Visualizer 频谱 Equalizer 均衡器 BassBoo ...
 - 我的Android进阶之旅------>Android关于Activity管理的一个简单封装
		
怎样管理当前的执行Activity栈,怎样彻底退出程序.本文封装了一个Activity管理类,能够方便随时退出程序. import java.util.Stack; import android.ap ...
 - 【我的Android进阶之旅】Android使用getIdentifier()方法根据资源名来获取资源id
		
有时候我们想动态的根据一个资源名获得到对应的资源id,就可以使用getResources().getIdentifier()方法来获取该id.然后再使用该id进行相关的操作. 1.Demo示例 下面用 ...
 
随机推荐
- [转]mysql视图学习总结
			
转自:http://www.cnblogs.com/wangtao_20/archive/2011/02/24/1964276.html 一.使用视图的理由是什么?1.安全性.一般是这样做的:创建一个 ...
 - asp.net mvc 最简单身份验证 [Authorize]通过的标准
			
[Authorize] public ContentResult Index2() { return Content("验证通过了"); } 经常能够看到某个Controler下的 ...
 - 实现一个类似于收趣APP的功能
			
近日想做一个类似于收趣APP软件的一个功能,将头条.微信等其他App的文章能够通过分享微信好友的方式分享到自己的平台软件中. 分享的方式有三种: 1.通过微信好友的方式,将文章分享给收趣. 2.复制文 ...
 - Python3之Zip
			
from collections import defaultdict from collections import OrderedDict d = defaultdict(list) d['a'] ...
 - Win32基础知识整理
			
1.定义字符串 在资源新建String table,增加新字符串: (win32加载) TCHAR tcIDName[255]=_T(""); LoadString(hInstan ...
 - CSS——img
			
img标签初始化:在低版本的ie浏览器会自带边框,所以建议border:0px.
 - SQL Server建库-建表-建约束
			
----------------------------------------SQL Server建库-建表-建约束创建School数据库------------------------------ ...
 - 12、scala函数式编程集合
			
1.Scala的集合体系结构 2.List 3.LikedList 4.Set 5.集合的函数式编程 6.函数式编程综合案例:统计单词总数 1.Scala的集合体系结构 Scala中集合体系主要包括: ...
 - Python 之字符串操作
			
# capitalize()将字符串的第一个字符转换为大写 # center(width, fillchar)返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格. ...
 - plsql developer连接oracle数据库
			
1.下载安装PLSQL Developer12 访问PLSQL Developer官网https://www.allroundautomations.com/bodyplsqldevreg.html, ...