当前位置: 首页 > article >正文

Android View 的绘制流程


view作为构成android界面的基本元素,深入了解view的绘制流程对开发人员来说是很有必要的。

我们创建一个Activity都会在onCreate方法中写setContentView(layoutResId),把我们的布局传进去,那此时我们的布局就添加到屏幕上了吗?当然不是的。那我们的这个布局是在什么时候添加进屏幕的呢?

在ActivityThread.java 的handleResumeActivity中

//ActivityThread.java
@Override
public void handleResumeActivity(ActivityClientRecord r, boolean finalStateRequest,boolean
isForward, String reason) {
//...
wm.addView(decor, l);
//...
}

这里调用wm的addView,才把DecorView添加到窗口,wm其实就是WindowManagerImpl,接着我们跟进到WindowManagerImpl的addView

//WindowManagerImpl.java
@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
    applyTokens(params);
    mGlobal.addView(view, params, mContext.getDisplayNoVerify(), mParentWindow,
    mContext.getUserId());
}

这里调用的是mGlobal的addView方法,mGlobal其实就是WindowManagerGlobal,是管理整个进程所有窗口信息的,接下来看看WindowManagerGlobal的addView方法

public void addView(View view, ViewGroup.LayoutParams params,
    Display display, Window parentWindow, int userId) {
    //...
    final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
    ViewRootImpl root;
    if (windowlessSession == null) {
        root = new ViewRootImpl(view.getContext(), display);
     } else {
         root = new ViewRootImpl(view.getContext(), display,windowlessSession);
     }
//为DecorView设置LayoutParams
    view.setLayoutParams(wparams);
    mViews.add(view);
    mRoots.add(root);
    mParams.add(wparams);

    root.setView(view, wparams, panelParentView, userId);
    //...  
}

接下来才到关键的地方了,ViewRootImpl的setView方法

public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView,
            int userId) {
    //...
    //遍历view树逻辑
    requestLayout();
    //...
    //将窗口添加到WMS上面
    res = mWindowSession.addToDisplayAsUser(mWindow, mWindowAttributes,
            getHostVisibility(),mDisplay.getDisplayId(), userId,
                            mInsetsController.getRequestedVisibilities(), inputChannel,
                            mTempInsets,mTempControls);
    //...
    //将ViewRootImpl设置为DecorView的parent
    view.assignParent(this);
}

在这里的requestLayout,最终会走到performTraversals方法,这里才真正开始遍历view的measure、layout、draw等流程

private void performTraversals() {
    //...
    //预测量,最多会执行3次onMeasure
    // Ask host how big it wants to be
    windowSizeMayChange |= measureHierarchy(host, lp, mView.getContext().getResources(),
    desiredWindowWidth, desiredWindowHeight);
    //...
    // Ask host how big it wants to be
    performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
    //...
    performLayout(lp, mWidth, mHeight);
    //...
    if (!performDraw() && mSyncBufferCallback != null) {
        mSyncBufferCallback.onBufferReady(null);
    }
    //...
}

预测量

private boolean measureHierarchy(final View host, final WindowManager.LayoutParams lp,
    final Resources res, final int desiredWindowWidth, final int desiredWindowHeight) {
    //...
  
    if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT) {
        final DisplayMetrics packageMetrics = res.getDisplayMetrics();
        //默认320dp
        res.getValue(com.android.internal.R.dimen.config_prefDialogWidth, mTmpValue, true);
        if (baseSize != 0 && desiredWindowWidth > baseSize) {
            childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width,lp.privateFlags);
            childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height,
                        lp.privateFlags);
            //第一次预测量
            performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
            if((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0 {
                goodMeasure = true;
            } else {
                //宽度不够则再将剩余空间一般给child继续测量
                baseSize = (baseSize + desiredWindowWidth) / 2;
                childWidthMeasureSpec = getRootMeasureSpec(baseSize,
                    lp.width,lp.privateFlags);
                performMeasure(childWdithMeasureSpec,childHeightMeasureSpec);
                if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) {
                        if (DEBUG_DIALOG) Log.v(mTag, "Good!");
                        goodMeasure = true;
                 }
            }
        }
    }
           //如果宽度还不满足,则将全部宽度给child去测量
        if (!goodMeasure) {
            childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width,
                    lp.privateFlags);
            childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height,
                    lp.privateFlags);
            performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
            if (mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight())
            {
                windowSizeMayChange = true;
            }
        }
}

测量

private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {
        if (mView == null) {
            return;
        }
        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");
        try {
            mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_VIEW);
        }
}
//View.java
public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
    //...
    //最终执行到对应view的onMeasure中
    onMeasure(widthMeasureSpec, heightMeasureSpec);
    //...
}

布局

//ViewRootImpl.java
private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,
            int desiredWindowHeight) {

    //...
    host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
    //...
}


//View.java
public void layout(int l, int t, int r, int b) {
    //...
    //在这里给左上右下去赋值,之后的getWidth和getHeight才能获取到值
    boolean changed = isLayoutModeOptical(mParent) ?
                setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
    //...
    //这里调用到对应view的onLayout方法
    onLayout(changed, l, t, r, b);
    //...
}

绘制

//ViewRootImpl.java
private boolean performDraw() {
    //...
    boolean canUseAsync = draw(fullRedrawNeeded, usingAsyncReport && mSyncBuffer);
    //...
}

private boolean draw(boolean fullRedrawNeeded, boolean forceDraw) {
    //...
    if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset,
        scalingRequired, dirty, surfaceInsets)) {
        return false;
    }
}

private boolean drawSoftware(Surface surface, AttachInfo attachInfo, int xoff, int yoff,
            boolean scalingRequired, Rect dirty, Rect surfaceInsets) {
    //...
    //执行到对应view的draw方法
    mView.draw(canvas);
    //...
}


http://www.kler.cn/a/318354.html

相关文章:

  • Spring Cloud Gateway(分发请求)
  • 《Django 5 By Example》阅读笔记:p76-p104
  • 1.两数之和-力扣(LeetCode)
  • Linux——GPIO输入输出裸机实验
  • 解锁微前端的优秀库
  • linux设置主机名
  • 获取参数
  • 如何在MATLAB中进行无线通信系统性能分析
  • 构建高效企业客户管理系统:SpringBoot应用
  • 旋转矩阵乘法,自动驾驶中的点及坐标系变换推导
  • Java调用数据库 笔记06 (修改篇)
  • 【JAVA开源】基于Vue和SpringBoot的图书馆管理系统
  • Spring自定义参数解析器
  • weblogic中间件漏洞复现
  • M2型TAM靶向肽CRV; Ahx-CRVLRSGSC ;
  • 【系统架构设计师】设计模式的分类
  • Python 中自动打开网页并点击[自动化脚本],Selenium
  • [Python]案例驱动最佳入门:Python数据可视化在气候研究中的应用
  • C++——用带有默认参数的函数实现,求两个整数或三个整数中的最大数。
  • QT客户端发送HTTP请求此时服务器到底收到了哪些数据?
  • fastson与jackson重点笔记(包入门)
  • Cilium + ebpf 系列文章- XDP (eXpress data Path)(四)
  • 基于jsonpath的JSON数据查找
  • golang学习笔记1-go程序执行流程
  • element-plus表格操作
  • C++之哈希 --- 哈希的应用(位图布隆过滤器)