今天學(xué)習(xí)Android VIEW的刷新機(jī)制,,之前一直以為是調(diào)用VIEW的刷新就自己調(diào)用刷新函數(shù),。今天學(xué)習(xí)了一下view的刷新機(jī)制,還是表示學(xué)習(xí)到很多的知識(shí)啊,。
感想就是自己要多閱讀android的源代碼,,其實(shí)很多的消息傳遞等等的機(jī)制,都是通過(guò)閱讀android的源代碼得到的,,所以有事沒(méi)事就去看源代碼玩吧~
好了,,來(lái)到正題,關(guān)鍵的一句話就是:
在Android的布局體系中,,父View負(fù)責(zé)刷新,、布局顯示子View;而當(dāng)子View需要刷新時(shí),,則是通知父View來(lái)完成,。
步驟就是:
1、調(diào)用子View的invalidate()
2,、跳轉(zhuǎn)到上一層的invalidateChild函數(shù)中區(qū)
3、在一次調(diào)用invalidateChildInParent的函數(shù)一次層層刷新
4、具體的刷新后續(xù)操作,,我就不清楚了,,調(diào)用invalidate最終在代碼上就在invalidateChild終止了的,所以表示有點(diǎn)點(diǎn)不清晰,,求各位大牛介紹一下吧,。。,。,。。,?在此謝過(guò)了,。。
讓我來(lái)閱讀源代碼:
首先在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的對(duì)象
final AttachInfo ai = mAttachInfo;//獲得匹配
if (p != null && ai != null) {
final Rect r = ai.mTmpInvalRect;
r.set(0, 0, mRight - mLeft, mBottom - mTop);//設(shè)置本View的尺寸,,其實(shí)就是大小沒(méi)有設(shè)置位置
// Don't call invalidate -- we don't want to internally scroll
// our own bounds
p.invalidateChild(this, r); //調(diào)用父類的刷新函數(shù)
}
}
}
下面我們來(lái)到Viewgroup對(duì)象:
在invalidate中,,調(diào)用父View的invalidateChild,這是一個(gè)從第向上回溯的過(guò)程,,每一層的父View都將自己的顯示區(qū)域與傳入的刷新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 && (view.mPrivateFlags & DIRTY_MASK) != DIRTY) {
view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag;
}
parent = parent.invalidateChildInParent(location, dirty);
} while (parent != null);
}
/**
* Don't call or override this method. It is used for the implementation of
* the view hierarchy.
*
* This implementation returns null if this ViewGroup does not have a parent,
* if this ViewGroup is already fully invalidated or if the dirty rectangle
* does not intersect with this ViewGroup's bounds.
*/
public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD_IN_PARENT);
}
if ((mPrivateFlags & DRAWN) == DRAWN) {
if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
FLAG_OPTIMIZE_INVALIDATE) {
// 由父類的的位置,偏移刷新區(qū)域
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;
}
另外:
Invalidate()方法不能放在線程中,,所以需要把Invalidate()方法放在Handler中,。在MyThread中只需要在規(guī)定時(shí)間內(nèi)發(fā)送一個(gè)Message給handler,當(dāng)Handler接收到消息就調(diào)用Invalidate()方法,。
postInvalidate()方法就可以放在線程中做處理,,就不需要Handler。
而上面的新線程MyThre可以放在OnCreate()中開(kāi)始,,也可以放在OnStart()中開(kāi)始,。
Invalidate()方法和postInvalidate()都可以在主線程中調(diào)用而刷新視圖。
Invalidate()方法在SDK中是這樣描述的:Invalidate the whole view. If the view is visible, onDraw(Canvas) 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 postInvalidate(). 當(dāng)Invalidate()被調(diào)用的時(shí)候,,View的OnDraw()就會(huì)被調(diào)用,,Invalidate()必須是在UI線程中被調(diào)用,如果在新線程中更新視圖的就調(diào)用postInvalidate(),。
簡(jiǎn)言之,,如果確定是在main thread中調(diào)用調(diào)用話, 使用 invaludate()
否則要調(diào)用 postInvalidate()
另外,,橫豎屏切換使用重新構(gòu)造 activity的,。所以一定會(huì)重新刷新view 。
|