Android View api 详解-LocationInWindow,getLocationOnScreen,getGlobalVisibleRect,getLocalVisibleRect
2017-08-14 17:08
1041 查看
概述
View 是很多控件的父类,因此掌握了 View api 有助于掌握其它控件。掌握LocationInWindow,getLocationOnScreen,getGlobalVisibleRect,getLocalVisibleRect的关键点:获得View在屏幕中的绝对位置。
点击查看 View 的官方 api
getLocationInWindow,getLocationOnScreen,getGlobalVisibleRect,getLocalVisibleRect
getLocationInWindowint[] position = new int[2]; textview.getLocationInWindow(position); System.out.println("getLocationInWindow:" + position[0] + "," + position[1]);
这个方法是将View的左上角坐标存入数组中.
此坐标是相对当前activity而言.
若是普通activity,则y坐标为可见的状态栏高度+可见的标题栏高度+view左上角到标题栏底部的距离.
可见的意思是:在隐藏了状态栏/标题栏的情况下,它们的高度以0计算.
若是对话框式的activity,则y坐标为可见的标题栏高度+view到标题栏底部的距离.
此时是无视状态栏的有无的.
getLocationOnScreen
int[] position = new int[2]; textview.getLocationOnScreen(position); System.out.println("getLocationOnScreen:" + position[0] + "," + position[1]);
这个方法跟上面的差不多,也是将view的左上角坐标存入数组中.但此坐标是相对整个屏幕而言.
y坐标为View左上角到屏幕顶部的距离.
getGlobalVisibleRect
Rect viewRect = new Rect(); textview.getGlobalVisibleRect(viewRect); System.out.println(viewRect);
这个方法是构建一个Rect用来”套”这个view.此Rect的坐标是相对当前activity而言.
若是普通activity,则Rect的top为可见的状态栏高度+可见的标题栏高度+Rect左上角到标题栏底部的距离.
若是对话框式的activity,则y坐标为Rect的top为可见的标题栏高度+Rect左上角到标题栏底部的距离.
此时是无视状态栏的有无的.
getGlobalVisibleRect
Rect rect = new Rect(); textview.getGlobalVisibleRect(rect); System.out.println(rect);
这个方法是构建一个Rect用来”套”这个view.
此Rect的坐标是相对当前activity而言.
若是普通activity,则Rect的top为可见的状态栏高度+可见的标题栏高度+Rect左上角到标题栏底部的距离.
若是对话框式的activity,则y坐标为Rect的top为可见的标题栏高度+Rect左上角到标题栏底部的距离.
此时是无视状态栏的有无的.
getLocalVisibleRect
Rect rect = new Rect(); button.getLocalVisibleRect(rect); System.out.println(rect);
getLocalVisibleRect的作用是获取视图本身可见的坐标区域,坐标以自己的左上角为原点(0,0).
点击查看getGlobalVisibleRect和getLocalVisibleRect详细对比
注意:以上方法在OnCreate方法中调用,都会返回0,这是因为View还未加载完毕;本文中点击计算之后才开始获取View的getLocationInWindow,getLocationOnScreen,getGlobalVisibleRect,getLocalVisibleRect值。
注:rect转化为String时第二个“,”变为“-”,从源码中可以看出
源码点击此处下载
相关文章推荐
- View坐标系详解(getTop(),getLeft(),getX(),getY(),getLocationOnScreen(), getLocationInWindow())
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- 我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的差别
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- Android 判断一个 View 是否可见 getLocalVisibleRect(rect) 与 getGlobalVisibleRect(rect)
- Android中getLocationOnScreen和getLocationInWindow
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- 我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的区别
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- android中getLocationInWindow 和 getLocationOnScreen的区别
- Android中getLocationOnScreen和getLocationInWindow 获取屏幕大小
- android中getLocationInWindow 和 getLocationOnScreen的区别
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- Android获取View位置getHitRect、getDrawingRect、getLocalVisibleRect、getGlobalVisibleRect说明
- View getLocationInWindow getLocationOnScreen
- Android中getLocationOnScreen和getLocationInWindow 获取屏幕大小
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen