View繪製基本流程
Android系統View的繪製流程:依次執行View類裏面的如下三個方法:
measure(int ,int) :測量View的大小
layout(int ,int ,int ,int) :設置子View的位置
draw(Canvas) :繪製View內容到Canvas畫布上
整個View樹的繪圖流程是在ViewRoot.Java類的performTraversals()函數展開的,該函數做的執行過程可簡單概況爲根據之前設置的狀態,判斷是否需要重新計算視圖大小(measure)、是否重新需要安置視圖的位置(layout)、以及是否需要重繪 (draw)
mesarue()測量過程
主要作用:爲整個View樹計算實際的大小,即設置實際的高(mMeasuredHeight)和寬(mMeasureWidth),每個View的控件的實際寬高都是由父視圖和本身視圖決定的。
具體的調用如下:
ViewRootImpl 的performTraversals方法中,調用measureHierarchy,然後調用performMeasure
private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");
try {
mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
}
}
ViewRoot根對象地屬性mView(其類型一般爲ViewGroup類型)調用measure()方法去計算View樹的大小,回調View/ViewGroup對象的onMeasure()方法,該方法實現的功能如下:
1、設置本View視圖的最終大小,該功能的實現通過調用setMeasuredDimension()方法去設置實際的高(mMeasuredHeight)和寬(mMeasureWidth)
2、如果該View對象是個ViewGroup類型,需要重寫onMeasure()方法,對其子視圖進行遍歷的measure()過程。
對每個子視圖的measure()過程,是通過調用父類ViewGroup.java類裏的measureChildWithMargins()方法去實現,該方法內部只是簡單地調用了View對象的measure()方法。
整個measure調用流程就是個樹形的遞歸過程measure()方法兩個參數都是父View傳遞過來的,也就是代表了父view的規格。他由兩部分組成,高2位表示MODE,定義在MeasureSpec類(View的內部類)中,有三種類型,MeasureSpec.EXACTLY表示確定大小, MeasureSpec.AT_MOST表示最大大小, MeasureSpec.UNSPECIFIED不確定。低30位表示size,也就是父View的大小。對於系統Window類的DecorVIew對象Mode一般都爲MeasureSpec.EXACTLY ,而size分別對應屏幕寬高。對於子View來說大小是由父View和子View共同決定的。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
boolean optical = isLayoutModeOptical(this);
if (optical != isLayoutModeOptical(mParent)) {
Insets insets = getOpticalInsets();
int opticalWidth = insets.left + insets.right;
int opticalHeight = insets.top + insets.bottom;
measuredWidth += optical ? opticalWidth : -opticalWidth;
measuredHeight += optical ? opticalHeight : -opticalHeight;
}
mMeasuredWidth = measuredWidth;
mMeasuredHeight = measuredHeight;
mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
}
layout佈局過程
主要作用 :爲將整個根據子視圖的大小以及佈局參數將View樹放到合適的位置上。
具體的調用如下:
ViewRootImpl 的performTraversals方法中,調用performLayout
private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,
int desiredWindowHeight) {
mLayoutRequested = false;
mScrollMayChange = true;
mInLayout = true;
final View host = mView;
if (DEBUG_ORIENTATION || DEBUG_LAYOUT) {
Log.v(TAG, "Laying out " + host + " to (" +
host.getMeasuredWidth() + ", " + host.getMeasuredHeight() + ")");
}
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "layout");
try {
host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
mInLayout = false;
int numViewsRequestingLayout = mLayoutRequesters.size();
if (numViewsRequestingLayout > 0) {
// requestLayout() was called during layout.
// If no layout-request flags are set on the requesting views, there is no problem.
// If some requests are still pending, then we need to clear those flags and do
// a full request/measure/layout pass to handle this situation.
ArrayList<View> validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters,
false);
if (validLayoutRequesters != null) {
// Set this flag to indicate that any further requests are happening during
// the second pass, which may result in posting those requests to the next
// frame instead
mHandlingLayoutInLayoutRequest = true;
// Process fresh layout requests, then measure and layout
int numValidRequests = validLayoutRequesters.size();
for (int i = 0; i < numValidRequests; ++i) {
final View view = validLayoutRequesters.get(i);
Log.w("View", "requestLayout() improperly called by " + view +
" during layout: running second layout pass");
view.requestLayout();
}
measureHierarchy(host, lp, mView.getContext().getResources(),
desiredWindowWidth, desiredWindowHeight);
mInLayout = true;
host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
mHandlingLayoutInLayoutRequest = false;
// Check the valid requests again, this time without checking/clearing the
// layout flags, since requests happening during the second pass get noop'd
validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, true);
if (validLayoutRequesters != null) {
final ArrayList<View> finalRequesters = validLayoutRequesters;
// Post second-pass requests to the next frame
getRunQueue().post(new Runnable() {
@Override
public void run() {
int numValidRequests = finalRequesters.size();
for (int i = 0; i < numValidRequests; ++i) {
final View view = finalRequesters.get(i);
Log.w("View", "requestLayout() improperly called by " + view +
" during second layout pass: posting in next frame");
view.requestLayout();
}
}
});
}
}
}
} finally {
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
}
mInLayout = false;
}
draw()繪圖過程
ViewRootImpl 的performTraversals方法中,調用了mView的draw方法
mView.draw()開始繪製,draw()方法實現的功能如下:
1 、繪製該View的背景
2 、爲顯示漸變框做一些準備操作
3、調用onDraw()方法繪製視圖本身 (每個View都需要重載該方法,ViewGroup不需要實現該方法)
4、調用dispatchDraw ()方法繪製子視圖(如果該View類型不爲ViewGroup,即不包含子視圖,不需要重載該方法)
值得說明的是,ViewGroup類已經爲我們重寫了dispatchDraw ()的功能實現,應用程序一般不需要重寫該方法,但可以重載父類函數實現具體的功能。
dispatchDraw()方法內部會遍歷每個子視圖,調用drawChild()去重新回調每個子視圖的draw()方法。
5、繪製滾動條
刷新視圖
Android中實現view的更新有兩個方法,一個是invalidate,另一個是postInvalidate,其中前者是在UI線程自身中使用,而後者在非UI線程中使用。
requestLayout()方法 :會導致調用measure()過程 和 layout()過程 。
說明:只是對View樹重新佈局layout過程包括measure()和layout()過程,不會調用draw()過程,但不會重新繪製
任何視圖包括該調用者本身。
一般引起invalidate()操作的函數如下:
1、直接調用invalidate()方法,請求重新draw(),但只會繪製調用者本身。
2、setSelection()方法 :請求重新draw(),但只會繪製調用者本身。
3、setVisibility()方法 : 當View可視狀態在INVISIBLE轉換VISIBLE時,會間接調用invalidate()方法,繼而繪製該View。
4 、setEnabled()方法 : 請求重新draw(),但不會重新繪製任何視圖包括該調用者本身。