UI绘制流程

182 阅读1分钟

1. Activity 中的window 也就是PhoneWindow类里面会按照下面的流程将View添加到DecorView上

其中里面的基础布局中的FrameLayout的id 是固定的 为android.R.id.content

2. 将DecorView 添加到Window上

window.addView 其中window的实现类是WindowManagerImpl ,它是一个包装类,真正干活的是WindowManagerGlobal ,最终由ViewRootImpl实现addView,此方法中又会调用requestLayout 从而引起界面的绘制

3. 模式+尺寸->MeasureSpec 32位int值 ,前两位是表示模式 后30位表示尺寸

    /**
     * Measure specification mode: The parent has not imposed any constraint
     * on the child. It can be whatever size it wants.
     */
    public static final int UNSPECIFIED = 0 << MODE_SHIFT; // 父容器不对view做任何限制一般是ListView 这种的使用

    /**
     * Measure specification mode: The parent has determined an exact size
     * for the child. The child is going to be given those bounds regardless
     * of how big it wants to be.
     */
    public static final int EXACTLY     = 1 << MODE_SHIFT; // 精确模式,父容器检测出View的大小,View的大小就是SpecSize 对应的LayoutParams就是match_parent 固定大小

    /**
     * Measure specification mode: The child can be as large as it wants up
     * to the specified size.
     */
    public static final int AT_MOST     = 2 << MODE_SHIFT;  // 父容器指定一个可用大小 ,View的大小不能超过这个大小 ,对应的LayoutParams 就是wrap_content

4. DecorView的MeasureSpec

就普通的手机的应用而言 就直接命中第一条了

5. View的MeasureSpec

(了解了貌似也没啥用)

6. View的布局

7. View的绘制