原文出处:http://blog.sina.com.cn/s/blog_55afbecd0100vqba.html
Android的view组件显示主要经过mesure, layout和draw这三个过程。在mesure阶段里调用mesure(int widthSpec, int heightSpec)方法,这个方法是final不能被重写,在这个过程里会调用onMesure(int widthSpec, int heightSpec)方法,这个方法可以重写。当组件设置好大小后,调用final layout(int l, int t, int r, int b)方法进行布局,在这个过程里会调用onLayout(boolean changed, int l, int t, int r, int b)方法,(l,t),(r,b)可以看成是组件左上角和右下角两个点的坐标,所以处理组件的布局通常要重写onMesure和onLayout这两个方 法。
getIntrinsicHeight()方法,然后设为背景。
===============================================
我们在自定义view的时候只需要重写onDraw(),而没必要去重写draw(),因为onDraw()是draw()方法的一部分。
大概扫一下源码就可以明白,draw()这个函数本身会做很多事情,
* 1. Draw the background
* 2. If necessary, save the canvas' layers to prepare for fading
* 3. Draw view's content
* 4. Draw children
* 5. If necessary, draw the fading edges and restore layers
* 6. Draw decorations (scrollbars for instance)
在第三步的时候,它就会调用onDraw()方法,来绘制view的内容。也就是draw会调用onDraw。
所以看需要,一般情况下,直接用onDraw绘制view的content就可以了,如果绘制多一点的内容,可以调用draw(),不过Android官 方推荐用只用onDraw就可以了。“When implementing a view, do not override this method; instead, you should implement onDraw”