android中Invalidate和postInvalidate的区别

2025-05-20 07:36:53
推荐回答(2个)
回答1:

Invalidate和postInvalidate和requestLayout都是用来刷新view控件的:
主线程可以直接调用Invalidate()方法刷新。
子线程可以直接调用postInvalidate()方法刷新。
API的描述是这样的 : Invalidatethe whole view. If the view is visible, onDraw(Canvas) will be called at somepoint in the future. This must be called from a UI thread. To call from anon-UI thread, call postInvalidate().。当Invalidate()被调用的时候,View的OnDraw()就会被调用,Invalidate()必须是在UI线程中被调用,如果在新线程中更新视图的就调用postInvalidate()。

回答2:

Invalidate UI线程自身中使用
postInvalidate 非UI线程中使用

就是你在别的线程里的时候不能操作UI,postInvalidate可以让这个操作到UI线程里执行