分享

Android View 的刷新机制

 昵称11494597 2013-03-28
在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); //调用父类的刷新函数
            }
        }
    }


本篇文章来源于 Linux公社网站(www.)  原文链接:http://www./Linux/2012-07/65023.htm


在Android的布局体系中,父View负责刷新、布局显示子View;而当子View需要刷新时,则是通知父View来完成。这种处理逻辑在View的代码中明确的表现出来:

public void invalidate() {
final ViewParent p = mParent;
final AttachInfo ai = mAttachInfo;
if (p != null && ai != null) {
final Rect r = ai.mTmpInvalRect;

// 设置刷新区域为自己的尺寸
r.set(0, 0, mRight - mLeft, mBottom - mTop);
p.invalidateChild(this, r);
}
}

子View调用invalidate时,首先找到自己父View(View的成员变量mParent记录自己的父View),然后将AttachInfo中保存的信息告诉父View刷新自己。

View的父子关系的建立分为两种情况:

1) View加入ViewGroup中

private void addViewInner(View child, int index, LayoutParams params, boolean preventRequestLayout) {

.....

// tell our children
if (preventRequestLayout) {
child.assignParent(this);
} else {
child.mParent = this;
}

.....

}

2)DecorView注册给WindowManagerImpl时,产生一个ViewRoot作为其父View。

public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView){

.....

view.assignParent(this);

....

}

AttachInfo是在View第一次attach到Window时,ViewRoot传给自己的子View的。这个AttachInfo之后,会顺着布局体系一直传递到最底层的View。

View.java

void dispatchAttachedToWindow(AttachInfo info, int visibility) {
mAttachInfo = info;

.....
}

ViewGroup.java

void dispatchAttachedToWindow(AttachInfo info, int visibility) {
super.dispatchAttachedToWindow(info, visibility);

for (int i = 0; i < count; i++) {
children[i].dispatchAttachedToWindow(info, visibility);
}
}

并且在新的View被加入ViewGroup时,也会将该AttachInfo传给加入的View

ViewGroup.java

private void addViewInner(View child, int index, LayoutParams params, boolean preventRequestLayout) {

child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));

}

到这里明白了mParent与AttachInfo代表的意义,可以继续刷新过程的分析。

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

public final void invalidateChild(View child, final Rect dirty) {
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 && (view.mPrivateFlags & DIRTY_MASK) != DIRTY) {
view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag;
}

parent = parent.invalidateChildInParent(location, dirty);
} while (parent != null);
}
}

public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
if ((mPrivateFlags & DRAWN) == DRAWN) {
if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
FLAG_OPTIMIZE_INVALIDATE) {

// 根据父View的位置,偏移刷新区域
dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX, location[CHILD_TOP_INDEX] - mScrollY);

final int left = mLeft;
final int top = mTop;

//计算实际可刷新区域
if (dirty.intersect(0, 0, mRight - left, mBottom - top) ||
(mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
mPrivateFlags &= ~DRAWING_CACHE_VALID;

location[CHILD_LEFT_INDEX] = left;
location[CHILD_TOP_INDEX] = top;
return mParent;
}
} else {
mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;

location[CHILD_LEFT_INDEX] = mLeft;
location[CHILD_TOP_INDEX] = mTop;

dirty.set(0, 0, mRight - location[CHILD_LEFT_INDEX],
mBottom - location[CHILD_TOP_INDEX]);

return mParent;
}
}

return null;
}

这个向上回溯的过程直到ViewRoot那里结束,由ViewRoot对这个最终的刷新区域做刷新。

ViewRoot.java

public void invalidateChild(View child, Rect dirty) {

scheduleTraversals();

}


http://blog.csdn.net/dragondog/article/details/6454551




 Android中UI(View)的刷新

14人收藏此文章, 我要收藏发表于2年前(2010-10-30 10:43) , 已有5650次阅读 ,共2个评论

看了很多资料,翻啊翻啊,似乎有些了解了。

Android中对View的更新有很多种方式,使用时要区分不同的应用场合。我感觉最要紧的是分清:多线程和双缓冲的使用情况。


现在可以尝试理解下面的模拟场景:

        两个人:一对夫妻,老公上班,老婆在家,现在他们都要吃饭。

     “不使用多线程和双缓冲”的情况是:老公在公司吃,老婆在家吃,互不干扰,吃就是了。

     “使用多线程和不使用双缓冲”的情况是:老婆做好饭,另外让人送一份到公司,老公收到饭就可以吃了。

     “使用多线程和使用双缓冲”的情况是:老婆做好饭,等老公回家一起吃。


1.不使用多线程和双缓冲

     这种情况最简单了,一般只是希望在View发生改变时对UI进行重绘。你只需在Activity中显式地调用View对象中的invalidate()方法即可。系统会自动调用 View的onDraw()方法。


2.使用多线程和不使用双缓冲

     这种情况需要开启新的线程,新开的线程就不好访问View对象了。强行访问的话会报:android.view.ViewRoot$CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its views.

     这时候你需要创建一个继承了android.os.Handler的子类,并重写handleMessage(Message msg)方法。android.os.Handler是能发送和处理消息的,你需要在Activity中发出更新UI的消息,然后再你的Handler(可以使用匿名内部类)中处理消息(因为匿名内部类可以访问父类变量, 你可以直接调用View对象中的invalidate()方法 )。也就是说:在新线程创建并发送一个Message,然后再主线程中捕获、处理该消息。


3.使用多线程和双缓冲

    Android中SurfaceView是View的子类,她同时也实现了双缓冲。你可以定义一个她的子类并实现SurfaceHolder.Callback接口。由于实现SurfaceHolder.Callback接口,新线程就不需要android.os.Handler帮忙了。SurfaceHolder中lockCanvas()方法可以锁定画布,绘制玩新的图像后调用unlockCanvasAndPost(canvas)解锁(显示),还是比较方便得。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多