View的绘制机制
前言
Android面试基本知识点->View的绘制机制,后续会完善。
提示:以下是本篇文章正文内容,下面案例可供参考
一、View树的绘制流程
整个View树的绘图流程是在ViewRoot.java类的performTraversals()函数展开的,该函数做的执行过程可简单概况为根据之前设置的状态,判断是否需要重新计算视图大小(measure)、是否重新需要安置视图的位置(layout)、以及是否需要重绘(draw)。
onMeasure -> onLayou -> onDraw
关于构造函数的调用
/**
* 构造函数会在代码new的时候调用
*/
public MyTextView(Context context) {
this(context, null);
}
/**
* 在布局layout中使用
*/
public MyTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
/**
* 在布局layout中使用,但是会有style
*/
public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
//调用attr中的style
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
mText = array.getString(R.styleable.MyTextView_text);
mTextSize = array.getDimensionPixelSize(R.styleable.MyTextView_textSize, mTextSize);
mTextColor = array