在Android中,我们有时候可能会将子View的坐标转换为父View中的坐标。 在Launcher中有这么一段代码可以完成这项工作。 publicfloatgetDescendantCoordRelativeToSelf(Viewdescendant,int[]coord){ floatscale=1.0f; float[]pt={coord[0],coord[1]}; //坐标值进行当前窗口的矩阵映射,比如View进行了旋转之类, // 它的坐标系会发生改变。map之后,会把点转换为改变之前的坐标。这个地方很关键 descendant.getMatrix().mapPoints(pt); //转换为直接父窗口的坐标 scale*=descendant.getScaleX(); pt[0]+=descendant.getLeft(); pt[1]+=descendant.getTop(); ViewParentviewParent=descendant.getParent(); //循环获得父窗口的父窗口,并且依次计算在每个父窗口中的坐标 while(viewParentinstanceofView&&viewParent!=this){ finalViewview=(View)viewParent; view.getMatrix().mapPoints(pt); scale*=view.getScaleX();//这个是计算X的缩放值。此处可以不管 //转换为相当于可视区左上角的坐标,scrollX,scollY是去掉滚动的影响 pt[0]+=view.getLeft()-view.getScrollX(); pt[1]+=view.getTop()-view.getScrollY(); viewParent=view.getParent(); } coord[0]=(int)Math.round(pt[0]); coord[1]=(int)Math.round(pt[1]); returnscale; }
其中, descendant 是子View, coord是在 descendant中的坐标值。而这个函数是父View的函数。当然,我们可以可以加一个 参数,指定父View。

更多相关文章

  1. android上一些方法的区别和用法的注意事项
  2. Android(安卓)--- Activity生命周期
  3. 锁屏界面
  4. android 零星调试笔记
  5. 从 Android(安卓)Sample ApiDemos 中学习 android.animation API
  6. Android常用控件
  7. Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明
  8. SlidingMenu和ActionBarSherlock结合做出出色的App布局,Facebook
  9. android上一些方法的区别和用法的注意事项

随机推荐

  1. 带你了解MySQL中的事件调度器EVENT
  2. MySQL索引的基本语法
  3. Windows系统下MySQL8.0.21安装教程(图文
  4. MySQL Installer 8.0.21安装教程图文详解
  5. Mysql最新版8.0.21下载安装配置教程详解
  6. MySQL是如何保证数据的完整性
  7. MySQL忘记root密码错误号码1045的解决办
  8. MySQL数据库查询性能优化策略
  9. mysql免安装版步骤解压后找不到密码处理
  10. mysql8.0.21下载安装详细教程