Android View重新整理機制
在Android的佈局體系中,父View負責重新整理、佈局顯示子View;而當子View需要重新整理時,則是通知父View來完成。這種處理邏輯在View的程式碼中明確的表現出來:
void invalidate(boolean invalidateCache) { 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); } } }
子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();
}
另外:
Invalidate()方法不能放線上程中,所以需要把Invalidate()方法放在Handler中。在MyThread中只需要在規定時間內傳送一個Message給handler,當Handler接收到訊息就呼叫Invalidate()方法。
postInvalidate()方法就可以放線上程中做處理,就不需要Handler。
而上面的新執行緒MyThread可以放在OnCreate()中開始,也可以放在OnStart()中開始。
Invalidate()方法和postInvalidate()都可以在主執行緒中呼叫而重新整理檢視。
Invalidate()方法在SDK中是這樣描述的:Invalidatethe whole view. If the view is visible, onDraw(Canvas) will be called at somepoint in the future. This must be called from a UI thread. To call from anon-UI thread, call postInvalidate(). 當Invalidate()被呼叫的時候,View的OnDraw()就會被呼叫,Invalidate()必須是在UI執行緒中被呼叫,如果在新執行緒中更新檢視的就呼叫postInvalidate()。