码迷,mamicode.com
首页 > 其他好文 > 详细

通过查看源码,查看invalidate和postInvalidate的区别

时间:2014-06-27 23:39:04      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:style   使用   os   cti   for   c   

查View的源码可知:


postInvalidate是使用handler发送消息,即可以使用在非UI线程中,而invalidate直接修改布局,所以只能用在UI线程里了。


public void invalidate() {
        invalidate(true);
    }


void invalidate(boolean invalidateCache) {
        if (ViewDebug.TRACE_HIERARCHY) {
            ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
        }

        if (skipInvalidate()) {
            return;
        }
        if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
                (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
                (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
            mLastIsOpaque = isOpaque();
            mPrivateFlags &= ~DRAWN;
            mPrivateFlags |= DIRTY;
            if (invalidateCache) {
                mPrivateFlags |= INVALIDATED;
                mPrivateFlags &= ~DRAWING_CACHE_VALID;
            }
            final AttachInfo ai = mAttachInfo;
            final ViewParent p = mParent;
            //noinspection PointlessBooleanExpression,ConstantConditions
            if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
                if (p != null && ai != null && ai.mHardwareAccelerated) {
                    // fast-track for GL-enabled applications; just invalidate the whole hierarchy
                    // with a null dirty rect, which tells the ViewAncestor to redraw everything
                    p.invalidateChild(this, null);
                    return;
                }
            }

            if (p != null && ai != null) {
                final Rect r = ai.mTmpInvalRect;
                r.set(0, 0, mRight - mLeft, mBottom - mTop);
                // Don‘t call invalidate -- we don‘t want to internally scroll
                // our own bounds
                p.invalidateChild(this, r);
            }
        }
    }


public void postInvalidate() {
        postInvalidateDelayed(0);
    }


 public void postInvalidateDelayed(long delayMilliseconds) {
        // We try only with the AttachInfo because there‘s no point in invalidating
        // if we are not attached to our window
        AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            Message msg = Message.obtain();
            msg.what = AttachInfo.INVALIDATE_MSG;
            msg.obj = this;
            attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
        }
    }



通过查看源码,查看invalidate和postInvalidate的区别,布布扣,bubuko.com

通过查看源码,查看invalidate和postInvalidate的区别

标签:style   使用   os   cti   for   c   

原文地址:http://blog.csdn.net/leehu1987/article/details/35276197

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!