requestLayout、forceLayout、invalidate、postInvalidate总结

 

postInvalidate 在非UI线程调用

invalidate()

Calling invalidate() is done when you want to schedule a redraw of the view. It will result in onDraw being called eventually (soon, but not immediately). An example of when a custom view would call it is when a text or background color property has changed.

The view will be redrawn but the size will not change.

调用invalidate会导致onDraw,不是立即.例如自定义view的text和背景颜色改变了。但是尺寸将会不变。

requestLayout()

如果你的view需要影响尺寸,调用requestLayout,会触发onMeasure和onLayout(它以及他的父view),但是不一定保证触发onDraw。

If something about your view changes that will affect the size, then you should call requestLayout(). This will trigger onMeasure and onLayout not only for this view but all the way up the line for the parent views.

Calling requestLayout() is not guaranteed to result in an onDraw (contrary to what the diagram in the accepted answer implies), so it is usually combined with invalidate().

invalidate();

requestLayout();

An example of this is when a custom label has its text property changed. The label would change size and thus need to be remeasured and redrawn.

forceLayout()

requestLayout()并不一定会导致子view的重新测量和重新排布,如果要求子view重新测量,则配合调用forceLayout;

When there is a requestLayout() that is called on a parent view group, it does not necessary need to remeasure and relayout its child views. However, if a child should be included in the remeasure and relayout, then you can call forceLayout() on the child. forceLayout() only works on a child if it occurs in conjunction with a requestLayout() on its direct parent. Calling forceLayout() by itself will have no effect since it does not trigger a requestLayout() up the view tree.

发布了29 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/s122ktyt/article/details/103724319