一 、invalidate()
源碼中的註釋:
/**
* Invalidate the whole view. If the view is visible,
* {@link #onDraw(android.graphics.Canvas)} will be called at some point in
* the future.
* <p>
* This must be called from a UI thread. To call from a non-UI thread, call
* {@link #postInvalidate()}.
*/
public void invalidate() {
invalidate(true);
}
翻譯一下:這個方法用於刷新整個View.如果View可見,那麼將會調用這個View的onDraw(Canvas canvas)方法。重點來了,這個方法必須在UI線程中調用。如果想要在非UI線程中刷新View可以調用 postInvalidate()。
當然,有道了以下,Invalidate的意思是“使…無效”。這裏可理解爲是原View無效,View的重新繪製。
二 、postInvalidate()
源碼中的註釋
/**
* <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
* Use this to invalidate the View from a non-UI thread.</p>
*
* <p>This method can be invoked from outside of the UI thread
* only when this View is attached to a window.</p>
*
* @see #invalidate()
* @see #postInvalidateDelayed(long)
*/
public void postInvalidate() {
postInvalidateDelayed(0);
}
翻譯以下:可以使用這個方法在非UI線程中更新View。