监听视图树 ViewTreeObserver 获取View的宽高
前奏:在哪里可以获取到View的宽高
官方文档简介
内部接口
- 回调方法 public void onDraw()
- Interface definition for a callback to be invoked when the view tree is about to将要 be drawn.
- At this point, views cannot be modified in any way.
- Unlike with OnPreDrawListener, this method cannot be used to cancel the current drawing pass.
- An OnDrawListener listener cannot be added or removed from this method.
OnGlobalFocusChangeListener:当在一个视图树中的焦点状态发生改变时回调
- 回调方法 public void onGlobalFocusChanged(View oldFocus, View newFocus);
- Interface definition for a callback to be invoked when the focus state焦点状态 within the view tree changes.
- When the view tree transitions from touch mode to non-touch mode, oldFocus is null.
- When the view tree transitions from non-touch mode to touch mode, newFocus is null.
- When focus changes in non-touch mode (without transition from or to touch mode) either oldFocus or newFocus can be null.
OnGlobalLayoutListener:当在一个视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变时回调
- 回调方法 public void onGlobalLayout();
- Interface definition for a callback to be invoked when the global layout state布局状态 or the visibility of views within the view tree changes.
OnPreDrawListener:当一个视图树将要绘制时回调
- 回调方法 public boolean onPreDraw(); Return true to proceed with the current drawing pass, or false to cancel.
- Interface definition for a callback to be invoked when the view tree is about to将要 be drawn.
- At this point, all views in the tree have been measured and given a frame.
- Clients can use this to adjust适应 their scroll bounds or even to request a new layout before drawing occurs.
OnScrollChangedListener:当一个视图树中的一些组件发生滚动时回调
- 回调方法 public void onScrollChanged();
- Interface definition for a callback to be invoked when something in the view tree has been scrolled滚动.
OnTouchModeChangeListener:当一个视图树的触摸模式发生改变时回调
- 回调方法 public void onTouchModeChanged(boolean isInTouchMode); True if the view hierarchy is now in touch mode, false otherwise.
- Interface definition for a callback to be invoked when the touch mode触摸方式 changes.
OnWindowAttachListener:
- 回调方法 public void onWindowAttached(); 和 public void onWindowDetached();
- Interface definition for a callback to be invoked when the view hierarchy视图数 is attached to and detached from its window.
OnWindowFocusChangeListener:
- 回调方法 public void onWindowFocusChanged(boolean hasFocus); Set to true if the window is gaining focus, false if it is losing focus.
- Interface definition for a callback to be invoked when the view hierarchy's window focus state窗口焦点 changes.
公共方法
- void addOnDrawListener(ViewTreeObserver.OnDrawListener listener):Register a callback to be invoked when the view tree is about to be drawn.
- void addOnGlobalFocusChangeListener(ViewTreeObserver.OnGlobalFocusChangeListener listener):Register a callback to be invoked when the focus state within the view tree changes.
- void addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener listener):Register a callback to be invoked when the global layout state or the visibility of views within the view tree changes
- void addOnPreDrawListener(ViewTreeObserver.OnPreDrawListener listener):Register a callback to be invoked when the view tree is about to be drawn
- void addOnScrollChangedListener(ViewTreeObserver.OnScrollChangedListener listener):Register a callback to be invoked when a view has been scrolled.
- void addOnTouchModeChangeListener(ViewTreeObserver.OnTouchModeChangeListener listener):Register a callback to be invoked when the invoked when the touch mode changes.
- void addOnWindowAttachListener(ViewTreeObserver.OnWindowAttachListener listener):Register a callback to be invoked when the view hierarchy is attached to a window.
- void addOnWindowFocusChangeListener(ViewTreeObserver.OnWindowFocusChangeListener listener):Register a callback to be invoked when the window focus state within the view tree changes.
- void removeGlobalOnLayoutListener(ViewTreeObserver.OnGlobalLayoutListener victim):This method was deprecated in API level 16. Use removeOnGlobalLayoutListener instead
- void removeOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener victim):Remove a previously installed global layout callback
- final void dispatchOnDraw():Notifies registered listeners that the drawing pass is about to start.
- final void dispatchOnGlobalLayout():Notifies registered listeners that a global layout happened.
- final boolean dispatchOnPreDraw():Notifies registered listeners that the drawing pass is about to start.
- boolean isAlive():Indicates指示、判断 whether this ViewTreeObserver is alive是否可用.
案例:获取View的宽高
}
监听视图树 ViewTreeObserver 获取View的宽高的更多相关文章
- 监听视图树 OnGlobalLayoutListener
背景 我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我们来看一下示例: 首先我们写一个控件 public class MyImageView extends ImageView ...
- 在渲染前获取 View 的宽高
在渲染前获取 View 的宽高 这是一个比较有意义的问题,或者说有难度的问题,问题的背景为:有时候我们需要在view渲染前去获取其宽高,典型的情形是,我们想在onCreate.onStart.onRe ...
- 用addOnGlobalLayoutListener获取View的宽高
首先,我们在onCreate方法里调用getHeight()和 getWidth()是不能正确获取View的宽高的,因为onCreate方法执行完了,我们定义的控件才会被onMeasure()度量,所 ...
- 通过View.post()获取View的宽高
在Android里,获取View宽高的时机是个老生常谈的话题了.众所周知,在Oncreate里直接调用View.getWidth或者View.getMeasuredWidth返回都是0.所以获取宽高时 ...
- Android查缺补漏(View篇)--在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0?
在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0 ? @Override protected void onCreate(Bundle savedInstanc ...
- JS获取元素的宽高以及offsetTop,offsetLeft等的属性值
基本介绍 $(obj).width()与$(obj).height() $(obj).width()与$(obj).height() :jquery方式获取元素的宽高,不包括滚动条与工具条 $(obj ...
- JS基础篇--JS获取元素的宽高以及offsetTop,offsetLeft等的属性值
$(obj).width()与$(obj).height() $(obj).width()与$(obj).height() :jquery方式获取元素的宽高,不包括滚动条与工具条 $(obj).wid ...
- js获取隐藏元素宽高的方法
网上有一些js获取隐藏元素宽高的方法,但是可能会存在某些情况获取不了. 例如: <!DOCTYPE html> <html lang="en"> <h ...
- js判断图片加载完成后获取图片实际宽高
通常,我们会用jq的.width()/.height()方法获取图片的宽度/高度或者用js的.offsetwidth/.offsetheight方法来获取图片的宽度/高度,但这些方法在我们通过样式设置 ...
随机推荐
- Python之路【第九篇】:面向对象进阶
阅读目录 一. isinstance(obj,cls)和issubclass(sub,super)二. 反射三. __setattr__,__delattr__,__getattr__四. 二次加工标 ...
- 理解 Python 中的元类
本文编程环境:Jupyter NoteBook python3 类也是对象 在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段.在 Python 中这一点仍然成立: class Objec ...
- CSUOJ 1726 你经历过绝望吗?两次!BFS+优先队列
Description 4月16日,日本熊本地区强震后,受灾严重的阿苏市一养猪场倒塌,幸运的是,猪圈里很多头猪依然坚强存活.当地15名消防员耗时一天解救围困的"猪坚强".不过与在废 ...
- Django-url路由映射与views逻辑处理
一.URL路由映射 路由映射模块,主要完成url与views视图函数的映射.当一个url请求到来时,会按照这个模块中的url地址从上到下进行匹配,如果匹配成功,将执行映射试图中的函数:反之将返回404 ...
- 开始一个Django项目的简单方法
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': '数据库名(你 ...
- [CF1086E]Beautiful Matrix(容斥+DP+树状数组)
给一个n*n的矩阵,保证:(1)每行都是一个排列 (2)每行每个位置和上一行对应位置不同.求这个矩阵在所有合法矩阵中字典序排第几.考虑类似数位DP的做法,枚举第几行开始不卡限制,那么显然之前的行都和题 ...
- Mistakes(Updating)
1.当调试时发现无法正常调用函数时,检查是否发生爆栈 对于每个栈仅有4MB的空间,开int只能开大约5*10^5. 大数组一定要开全局变量 2.当long long=int*int时会爆int,一定要 ...
- Android MediaCodec 的实例化方法
*由于作者水平限制,文中难免有错误和不恰当之处,望批评指正. *转载请注明出处:http://www.cnblogs.com/roger-yu/ MediaCodec的实例化方法主要有两种: 1.使用 ...
- UVA 350 Pseudo-Random Numbers
Pseudo-Random Numbers Computers normally cannot generate really random numbers, but frequently are ...
- tyvj 1044 数字三角形 记忆化搜索
数字三角形 Time Limit: 1 Sec Memory Limit: 162 MB 题目连接 http://www.tyvj.cn/p/1044 Description 示出了一个数字三角形. ...