我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的差别
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的差别,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3074字,纯文字阅读大概需要5分钟。
内容图文
![我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的差别](/upload/InfoBanner/zyjiaocheng/1102/5d1f6fb909844ece9a8fe19d37849b68.jpg)
一个控件在其父窗体中的坐标位置
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的差别
原文:http://www.cnblogs.com/yangykaifa/p/7123502.html
内容总结
以上是互联网集市为您收集整理的我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的差别全部内容,希望文章能够帮你解决我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的差别所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。