1、invalidate()和requestLayout()主要用于视图的重绘和更新,区别如下:
invalidate()会执行onDraw()方法,即使什么都没有发生改变,它也会重新绘制。
requestLayout()会执行onMeasure()和onLayout()
所以我们进行视图更新的时候,若:
2、invalidate()和postInvalidate()均用于重新绘制视图,区别如下:
void requestLayout ()
Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree. This should not be called while the view hierarchy is currently in a layout pass (isInLayout(). If layout is happening, the request may be honored at the end of the current layout pass (and then layout will run again) or after the current frame is drawn and the next layout occurs.
Subclasses which override this method should call the superclass method to handle possible request-during-layout errors correctly.
void invalidate ()
Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future.
void postInvalidate ()
Cause an invalidate to happen on a subsequent cycle through the event loop. Use this to invalidate the View from a non-UI thread.
This method can be invoked from outside of the UI thread only when this View is attached to a window.
1、Android中实现view的更新有两种方法,一种是invalidate,另一种是postInvalidate,invalidate是在UI线程自身中使用,而postInvalidate在非UI线程中使用。
2、当view确定自身已经不再适合现有的区域时,该view本身调用这个方法要求parent view(父类的视图)重新调用他的onMeasure onLayout来重新设置自己位置。特别是当view的layoutparameter发生改变,并且它的值还没能应用到view上时,这时候适合调用requestLayout方法。