android view绘制

先看看 view 基于整个屏幕分布的概况


20150528211309106.png

首先要明确view是屏幕绘制的入口

那么View的绘制是从哪里开始的呢,我们知道每个Activity 均会创建一个PhoneWindow对象,
是Activity和整个View系统交互的接口,每个Window都对应着一个View和一个ViewRootImpl,
Window和View通过ViewRootImpl来建立联系,对于Activity来说,ViewRootImpl是连接
WindowManager和DecorView的纽带,绘制的入口是由ViewRootImpl的performTraversals方法
来发起Measure,Layout,Draw等流程的。。

再来个图来补充一下PhoneWindow:

503290-20151104115213477-1270385950.jpg

整个流程的大致流程图:

20150529090922419.png

看看源码:

private void performTraversals() {
        ......
        //最外层的根视图的widthMeasureSpec和heightMeasureSpec由来
        //lp.width和lp.height在创建ViewGroup实例时等于MATCH_PARENT
        int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
        int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
        ......
        mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        ......
        mView.layout(0, 0, mView.getMeasuredWidth(), mView.getMeasuredHeight());
        ......
        mView.draw(canvas);
        ......
    }

概况了解清楚了那么,来具体看看

measure()、layout()、draw()的具体流程

measure()

首先看看其整体的过程


20150529163050000.png

开始measure之前要明确一个概念MeasureSpec(测量规格),MeasureSpec是一个大小跟模式的组合值,控件自身的大小是受到MeasureSpec下的mode,size两个值共同来决定的

  @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        widthMode=MeasureSpec.getMode(widthMeasureSpec);
        hightMode=MeasureSpec.getMode(heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        hight = MeasureSpec.getSize(heightMeasureSpec);
    }

就是在子控件正要放置自己的位置时,父控件会通过MeasureSpec这个对象来问子控件,“你想要用多大地方啊?”;然后子控件通过一系列的操作来放置控件!

MeasureSpec的mode一共有三种模式:

UPSPECIFIED : 父容器对于子容器没有任何限制,子容器想要多大就多大
EXACTLY: 父容器已经为子容器设置了尺寸,子容器应当服从这些边界,不论子容器想要多大的空间。
AT_MOST:子容器可以是声明大小内的任意大小

根据mode,size 共同决定的子控件最终的大小:

父View的MeasureSpec 是EXACTLY,说明父View的大小是确切的,(确切的意思很好理解,如果一个View的MeasureSpec 是EXACTLY,那么它的size 是多大,最后展示到屏幕就一定是那么大)。

父View的MeasureSpec 是AT_MOST,说明父View的大小是不确定,最大的大小是MeasureSpec 的size值,不能超过这个值。

父View的MeasureSpec 是UNSPECIFIED(未指定),表示没有任何束缚和约束,不像AT_MOST表示最大只能多大,不也像EXACTLY表示父View确定的大小,子View可以得到任意想要的大小,不受约束

503290-20151119171702515-1341567594.png

好了 这是大概的流程,下面来个具体的示例:
布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"    
   android:id="@+id/linear"
   android:layout_width="match_parent"    
   android:layout_height="wrap_content"    
   android:layout_marginTop="50dp"    
   android:background="@android:color/holo_blue_dark"    
   android:paddingBottom="70dp"    
   android:orientation="vertical">    
   <TextView        
    android:id="@+id/text"       
    android:layout_width="match_parent"     
    android:layout_height="wrap_content"  
    android:background="@color/material_blue_grey_800"       
    android:text="TextView"        
    android:textColor="@android:color/white"        
    android:textSize="20sp" />    
   <View       
      android:id="@+id/view"       
     android:layout_width="match_parent" 
     android:layout_height="150dp"    
     android:background="@android:color/holo_green_dark" />
</LinearLayout>

屏幕显示样式如下:

966283-4a11f92ac8c5e224.png

整个图是一个DecorView,DecorView可以理解成整个页面的View,DecorView是一个FrameLayout,包含两个子View,一个id=statusBarBackground的View和一个是LineaLayout,id=statusBarBackground的View,而这个LinearLayout比较重要,它包含一个title和一个content,title很好理解其实就是TitleBar或者ActionBar,content 就更简单了,setContentView()方法你应该用过吧,android.R.id.content 你应该听过吧,没错就是它,content是一个FrameLayout,你写的页面布局通过setContentView加进来就成了content的直接子View。

966283-4096801e91e2eccc.png

绘制的流程是这样、详情请参考
http://www.jianshu.com/p/5a71014e7b1b

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容