invalidate()和postInvalidate() 的区别

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_25859403/article/details/52015055

一 、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。

猜你喜欢

转载自blog.csdn.net/qq_25859403/article/details/52015055
今日推荐