监听视图树 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方法来获取图片的宽度/高度,但这些方法在我们通过样式设置 ...
随机推荐
- Java 集合之 Map
Map 就是另一个顶级接口了,总感觉 Map 是 Collection 的子接口呢.Map 主要用于表示那些含有映射关系的数据,存储的是一组一组的键值对.Map 是允许你将某些对象与其它一些对象关联起 ...
- django中缓存配置
# ======缓存配置====== CACHES = { ## 虚拟缓存,开发调试版本,此为开始调试用,实际内部不做任何操作 # 'default': { # 'BACKEND': 'django. ...
- 【BZOJ 2986】 莫比乌斯函数+容斥原理
2986: Non-Squarefree Numbers Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 337 Solved: 156 Descri ...
- android OOM 内存溢出
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 一个应用的可用内存是有限的,如果超过了可用的内存,就会内存溢出. 1,避免 已经不用的对 ...
- 1265. [NOIP2012] 同余方程
1265. [NOIP2012] 同余方程 ★☆ 输入文件:mod.in 输出文件:mod.out 简单对比 时间限制:1 s 内存限制:128 MB [题目描述] 求关于 x 的同余 ...
- luoguP4696 [CEOI2011]Matching KMP+树状数组
可以非常轻易的将题意转化为有多少子串满足排名相同 注意到$KMP$算法只会在当前字符串的某尾添加和删除字符 因此,如果添加和删除后面的字符对于前面的字符没有影响时,我们可以用$KMP$来模糊匹配 对于 ...
- BZOJ 1528 [POI2005]sam-Toy Cars(优先队列)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1528 [题目大意] 地上最多可以放k个玩具,现在给出需求顺序, 问最少需要去架子上拿几 ...
- Java 接口与抽象类
抽象类 <JAVA编程思想>一书中,将抽象类定义为"包含抽象方法的类".只要用abstract修饰的类就是抽象类,抽象类不一定包含抽象方法,但有抽象方法的类一定是抽象类 ...
- django: ListView解读
[转载注明出处: http://www.cnblogs.com/yukityan/p/8039041.html ] django内置列表视图: # 导入 from django.views.gener ...
- Null 和 Undefined
在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? Undef ...