博客
关于我
android中getLocationInWindow 和 getLocationOnScreen的区别
阅读量:361 次
发布时间:2019-03-04

本文共 2687 字,大约阅读时间需要 8 分钟。

一个控件在其父窗口中的坐标位置

.getLocationInWindow(int[] location)




一个控件在其整个屏幕上的坐标位置

.getLocationOnScreen(int[] location)





getLocationInWindow是以B为原点的C的坐标

getLocationOnScreen以A为原点。


下面是getLocationOnScreen示例

[java] 
  1. start = (Button) findViewById(R.id.start);  
  2.         int []location=new int[2];  
  3.         start.getLocationOnScreen(location);  
  4.         int x=location[0];//获取当前位置的横坐标  
  5.         int y=location[1];//获取当前位置的纵坐标  


下面是getLocationInWindow示例

[java] 
  1. start = (Button) findViewById(R.id.start);  
  2.         int []location=new int[2];  
  3.         start.getLocationInWindow(location);  
  4.         int x=location[0];//获取当前位置的横坐标  
  5.         int y=location[1];//获取当前位置的纵坐标  

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

 附上源代码

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

.getLocationInWindow(int[] location)

[java] 
  1. /** 
  2.      * <p>Computes the coordinates of this view in its window. The argument 
  3.      * must be an array of two integers. After the method returns, the array 
  4.      * contains the x and y location in that order.</p> 
  5.      * 
  6.      * @param location an array of two integers in which to hold the coordinates 
  7.      */  
  8.     public void getLocationInWindow(int[] location) {  
  9.         if (location == null || location.length < 2) {  
  10.             throw new IllegalArgumentException("location must be an array of two integers");  
  11.         }  
  12.   
  13.         if (mAttachInfo == null) {  
  14.             // When the view is not attached to a window, this method does not make sense  
  15.             location[0] = location[1] = 0;  
  16.             return;  
  17.         }  
  18.   
  19.         float[] position = mAttachInfo.mTmpTransformLocation;  
  20.         position[0] = position[1] = 0.0f;  
  21.   
  22.         if (!hasIdentityMatrix()) {  
  23.             getMatrix().mapPoints(position);  
  24.         }  
  25.   
  26.         position[0] += mLeft;  
  27.         position[1] += mTop;  
  28.   
  29.         ViewParent viewParent = mParent;  
  30.         while (viewParent instanceof View) {  
  31.             final View view = (View) viewParent;  
  32.   
  33.             position[0] -= view.mScrollX;  
  34.             position[1] -= view.mScrollY;  
  35.   
  36.             if (!view.hasIdentityMatrix()) {  
  37.                 view.getMatrix().mapPoints(position);  
  38.             }  
  39.   
  40.             position[0] += view.mLeft;  
  41.             position[1] += view.mTop;  
  42.   
  43.             viewParent = view.mParent;  
  44.          }  
  45.   
  46.         if (viewParent instanceof ViewRootImpl) {  
  47.             // *cough*  
  48.             final ViewRootImpl vr = (ViewRootImpl) viewParent;  
  49.             position[1] -= vr.mCurScrollY;  
  50.         }  
  51.   
  52.         location[0] = (int) (position[0] + 0.5f);  
  53.         location[1] = (int) (position[1] + 0.5f);  
  54.     }  
.getLocationOnScreen(int[] location)

[java] 
  1. /** 
  2.    * <p>Computes the coordinates of this view on the screen. The argument 
  3.    * must be an array of two integers. After the method returns, the array 
  4.    * contains the x and y location in that order.</p> 
  5.    * 
  6.    * @param location an array of two integers in which to hold the coordinates 
  7.    */  
  8.   public void getLocationOnScreen(int[] location) {  
  9.       getLocationInWindow(location);  
  10.   
  11.       final AttachInfo info = mAttachInfo;  
  12.       if (info != null) {  
  13.           location[0] += info.mWindowLeft;  
  14.           location[1] += info.mWindowTop;  
  15.       }  
  16.   }  

 


你可能感兴趣的文章
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>
mysql 用户管理和权限设置
查看>>
MySQL 的 varchar 水真的太深了!
查看>>