• android View和ViewGroup创建以及绘制流程


    View:

    View(Context context, @Nullable AttributeSet attrs) ------ onFinishInflate() xml布局加载完成 -------

    requestLayout() -------- setLayoutParams(ViewGroup.LayoutParams params) ---------

    getLayoutParams() -------requestLayout()  -------  invalidateOutline() -------

    onRtlPropertiesChanged(int layoutDirection)  --------  onAttachedToWindow() ------

    onWindowVisibilityChanged(int visibility) -------   onVisibilityAggregated(boolean isVisible)  ------ 

    onVisibilityChanged(@NonNull View changedView, int visibility)  ------

    onCreateDrawableState(int extraSpace)  -------  hasWindowFocus()  ------   getLayoutParams() ---

    onMeasure(int widthMeasureSpec, int heightMeasureSpec) ------ 

    onSizeChanged(int w, int h, int oldw, int oldh) ----   

     onLayout(boolean changed, int left, int top, int right, int bottom)  -------

    layout(int l, int t, int r, int b) ------  onDraw(Canvas canvas) ---- dispatchDraw(Canvas canvas) -----

    onDrawForeground(Canvas canvas) ------   draw(Canvas canvas)  ------

    onCreateDrawableState(int extraSpace)  ----- hasWindowFocus() ----- 

    onWindowFocusChanged(boolean hasWindowFocus)   -----

    dispatchWindowFocusChanged(boolean hasFocus)  

    ViewGroup:

    CustomViewGroup(Context context, AttributeSet attrs) -----  

    checkLayoutParams(LayoutParams p)  ------ onFinishInflate() xml布局加载完成 ---- 

    onViewAdded(View child) ------  getLayoutDirection() ----  requestLayout() ------

    setLayoutParams(LayoutParams params) ----- getLayoutParams() -----   

    getLayoutDirection() ------  canResolveLayoutDirection() ------

    onRtlPropertiesChanged(int layoutDirection) -----   onAttachedToWindow() -------

    onWindowVisibilityChanged(int visibility) ------  onVisibilityAggregated(boolean isVisible) ------

    onVisibilityChanged(@NonNull View changedView, int visibility) -----  

    onCreateDrawableState(int extraSpace) ------  hasWindowFocus() ----- 

    getLayoutParams() ------  onMeasure(int widthMeasureSpec, int heightMeasureSpec) ----  

    onSizeChanged(int w, int h, int oldw, int oldh) --- onLayout(boolean changed, int l, int t, int r, int b)

    -----  onDraw(Canvas canvas) -----  dispatchDraw(Canvas canvas) -----

    onDrawForeground(Canvas canvas) ----  draw(Canvas canvas) ----- 

    onCreateDrawableState(int extraSpace)  ----  hasWindowFocus() ----- 

    onWindowFocusChanged(boolean hasWindowFocus) ----  

    dispatchWindowFocusChanged(boolean hasFocus) ---

    一般自定义控件会继承View 和ViewGroup 进行扩展,红色的方法主要与绘制控件大小颜色文字等相关,其中绘制主要是 measure、layout、draw,

    measure:测量View的宽高,主要是View中的measure(),setMeasuredDimension(),    onMeasure()方法;

    layout:计算当前View以及子View的位置,主要是View中的:layout(),onLayout(),setFrame()方法;

    draw:视图的绘制工作,主要是View中的:onDraw(),draw() 方法。

    measure中很重要的一个类MeasureSpec:

    MeasureSpec是View的内部类,它封装了一个View的尺寸,在onMeasure()当中会根据这个MeasureSpec的值来确定View的宽高。MeasureSpec的值保存在一个int值当中。一个int值有32位,前两位表示模式mode后30位表示大小size。即MeasureSpec = mode + size。

    在MeasureSpec当中一共存在三种mode:

    • UNSPECIFIED:无限制,View对尺寸没有任何限制,View设置为多大就应当为多大。
    • EXACTLY :精准模式,View需要一个精确值,这个值即为MeasureSpec当中的Size。对应的是match_parent。
    • AT_MOST:最大模式,View的尺寸有一个最大值,View不可以超过MeasureSpec当中的Size值。对应的是wrap_content。
    1. // 获取测量模式(Mode)
    2. int specMode = MeasureSpec.getMode(measureSpec)
    3. // 获取测量大小(Size)
    4. int specSize = MeasureSpec.getSize(measureSpec)
    5. // 通过Mode 和 Size 生成新的SpecMode
    6. int measureSpec=MeasureSpec.makeMeasureSpec(size, mode);

    这里只记录了控件的创建和绘制过程,实际上自定义控件往往还要考虑事件的处理,所以定义一个负责的控件难度还是不小的。

    相关链接:

    android事件传递机制的浅谈_Alex老夫子的博客-CSDN博客

    深入理解Android之View的绘制流程_Alex老夫子的博客-CSDN博客

  • 相关阅读:
    Windows 10重新安装微软商店Microsoft Store
    使用docker搭建gogs
    超分辨率提升IRN网络
    networking /etc/network/interfaces 笔记221102
    【HTML】HTML基础6.1(表格以及常见属性)
    acwing算法基础之基础算法--整数离散化算法
    创建数据库
    快递地址自动识别填充
    考过HCIP入职心仪公司,分享华为认证学习经历及心得
    IDL学习——外部方法调用IDL.pro文件
  • 原文地址:https://blog.csdn.net/msn465780/article/details/126590153