当前位置:操作系统 > 安卓/Android >>

Android View 的刷新机制

今天学习android VIEW的刷新机制,之前一直以为是调用VIEW的刷新就自己调用刷新函数。今天学习了一下view的刷新机制,还是表示学习到很多的知识啊。

 

感想就是自己要多阅读android的源代码,其实很多的消息传递等等的机制,都是通过阅读android的源代码得到的,所以有事没事就去看源代码玩吧~

 

好了,来到正题,关键的一句话就是:

 

在Android的布局体系中,父View负责刷新、布局显示子View;而当子View需要刷新时,则是通知父View来完成。

 

步骤就是:

1、调用子View的invalidate()

2、跳转到上一层的invalidateChild函数中区

3、在一次调用invalidateChildInParent的函数一次层层刷新

4、具体的刷新后续操作,我就不清楚了,调用invalidate最终在代码上就在invalidateChild终止了的,所以表示有点点不清晰,求各位大牛介绍一下吧。。。。。?在此谢过了。。

 

让我来阅读源代码:

首先在View类中:

 

    /**
     * Invalidate the whole view. If the view is visible, {@link #onDraw} will
     * be called at some point in the future. This must be called from a
     * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
     */
    public void invalidate() {
        if (ViewDebug.TRACE_HIERARCHY) {
            ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
        }

        if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS)) {
            mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;

            final ViewParent p = mParent; //获得父类View的对象
            final AttachInfo ai = mAttachInfo;//获得匹配
            if (p != null && ai != null) {
                final Rect r = ai.mTmpInvalRect;  
                r.set(0, 0, mRight - mLeft, mBottom - mTop);//设置本View的尺寸,其实就是大小没有设置位置

                // Don't call invalidate -- we don't want to internally scroll
                // our own bounds
                p.invalidateChild(this, r);  //调用父类的刷新函数
            }
        }
    }

 

 

下面我们来到Viewgroup对象:

在invalidate中,调用父View的invalidateChild,这是一个从第向上回溯的过程,每一层的父View都将自己的显示区域与传入的刷新Rect做交集。

 /**
     * Don't call or override this method. It is used for the implementation of
     * the view hierarchy.
     */
    public final void invalidateChild(View child, final Rect dirty) {
        if (ViewDebug.TRACE_HIERARCHY) {
            ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD);
        }

        ViewParent parent = this;
        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            final int[] location = attachInfo.mInvalidateChildLocation;

            // 刷新子View的位置
            location[CHILD_LEFT_INDEX] = child.mLeft;
            location[CHILD_TOP_INDEX] = child.mTop;

            // If the child is drawing an animation, we want to copy this flag onto
            // ourselves and the parent to make sure the invalidate request goes
            // through
            final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION;

            // Check whether the child that requests the invalidate is fully opaque
            final boolean isOpaque = child.isOpaque() && !drawAnimation &&
                    child.getAnimation() != null;
            // Mark the child as dirty, using the appropriate flag
            // Make sure we do not set both flags at the same time
            final int opaqueFlag = isOpaque ? DIRTY_OPAQUE : DIRTY;
            do {
                View view = null;
                if (parent instanceof View) {
                    view = (View) parent;
                }

                if (drawAnimation) {
                    if (view != null) {
                        view.mPrivateFlags |= DRAW_ANIMATION;
                    } else if (parent instanceof ViewRoot) {
                        ((ViewRoot) parent).mIsAnimating = true;
                    }
                }

                // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
                // flag coming from the child that initiated the invalidate
                if (view != null &&am

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,