
android中getLocationInWindow 和 getLocationOnScreen的区别
.getLocationOnScreen(int[] location)
发布日期:2021-05-07 19:03:31
浏览次数:8
分类:技术文章
本文共 2687 字,大约阅读时间需要 8 分钟。
一个控件在其父窗口中的坐标位置
.getLocationInWindow(int[] location)
一个控件在其整个屏幕上的坐标位置
.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];//获取当前位置的纵坐标
==================================================================================================
附上源代码
==================================================================================================
.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);
- }
- /**
- * <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;
- }
- }
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月14日 14时01分25秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Dijkstra算法输出最短路径
2019-03-04
Dijkstra算法的总结
2019-03-04
zoj 3964 - Yet Another Game of Stones
2019-03-04
前后端通信问题 —— SpringBoot+LayUI
2019-03-04
ubuntu中安装scikit-learn
2019-03-04
Ubuntu2004 向日葵安装笔记
2019-03-04
Ubuntu 安装后无法正常打开——进入grub安全命令行模式
2019-03-04
C/C++ new和delete使用注意事项
2019-03-04
Jmeter (一) ----环境搭建
2019-03-04
性能调优优化思路
2019-03-04
CodeBase(四)项目总结
2019-03-04
【ACM】HDU 5640 King‘s Cake
2019-03-04
java集合框架
2019-03-04
面向对象的三大特征
2019-03-04
SpringCloud和SprinBoot之间的关系
2019-03-04
奇怪的小东西
2019-03-04
剑指offer打卡Day14:数组中只出现一次的数字
2019-03-04
使用VSCode配合keil来编写Cortex-M程序
2019-03-04
电磁兼容的PCB设计(二)
2019-03-04
i.mx rt系列遇害笔记-----systick被gpio害了
2019-03-04