http://orgcent.com/android-custom-view-draw-mechanism/
Android系統中要自定義view,首先需要了解Android的view加載機制。主要有三個方法:
1、onMeasure() //計算出view自身大小 2、onLayout() //僅在ViewGroup中,用來爲子view指定位置(left,top) 3、onDraw() //view繪製內容
那麼系統能讓我們在onDraw()能夠繪製些什麼呢,查看View.draw()源碼發現:
點擊查看源碼
下面根據源碼中的相關說明,進一步分析控件的繪製操作及順序:
* Draw traversal performs several drawing steps which must be executed
* in the appropriate order:
*
* 1. Draw the background (繪製控件設置的背景,系統已在view.draw()中繪製,只要在xml中指定背景即可)
* 2. If necessary, save the canvas' layers to prepare for fading
* 3. Draw view's content (可以重寫, onDraw(canvas);)
* 4. Draw children (可重寫,用來分發canvas到子控件,具體看ViewGroup。對應方法dispatchDraw(canvas);此方法依次調用了子控件的draw()方法)
* 5. If necessary, draw the fading edges and restore layers (繪製控件四周的陰影漸變效果)
* 6. Draw decorations (scrollbars for instance) (用來繪製滾動條,對應方法onDrawScrollBars(canvas);。
* onDrawHorizontalScrollBar()和onDrawVerticalScrollBar()被隱藏了無法重寫,也許有其他方法重寫滾動條)
*/