博客
关于我
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中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>