美文网首页
invalidate() 与 postInvalidate()看

invalidate() 与 postInvalidate()看

作者: 贝贝beibei96 | 来源:发表于2018-05-05 22:15 被阅读31次

    作用都是重绘界面

    贴上源码

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

    相关文章

      网友评论

          本文标题:invalidate() 与 postInvalidate()看

          本文链接:https://www.haomeiwen.com/subject/nmaerftx.html