invalidate() 与 postInvalidate()看

2018-05-05  本文已影响31人  贝贝beibei96

作用都是重绘界面

贴上源码

invalidate()

在UI线程调用

/**
     * 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);
    }

postInvalidate()

在非UI线程调用, 查看源码会发现, 底层也是通过调用invalidate()方法来实现重绘UI的

/**
     * <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);
    }
上一篇 下一篇

猜你喜欢

热点阅读