android view繪制

先看看 view 基于整個(gè)屏幕分布的概況


20150528211309106.png

首先要明確view是屏幕繪制的入口

那么View的繪制是從哪里開始的呢,我們知道每個(gè)Activity 均會(huì)創(chuàng)建一個(gè)PhoneWindow對象,
是Activity和整個(gè)View系統(tǒng)交互的接口,每個(gè)Window都對應(yīng)著一個(gè)View和一個(gè)ViewRootImpl,
Window和View通過ViewRootImpl來建立聯(lián)系,對于Activity來說,ViewRootImpl是連接
WindowManager和DecorView的紐帶,繪制的入口是由ViewRootImpl的performTraversals方法
來發(fā)起Measure,Layout,Draw等流程的。。

再來個(gè)圖來補(bǔ)充一下PhoneWindow:

503290-20151104115213477-1270385950.jpg

整個(gè)流程的大致流程圖:

20150529090922419.png

看看源碼:

private void performTraversals() {
        ......
        //最外層的根視圖的widthMeasureSpec和heightMeasureSpec由來
        //lp.width和lp.height在創(chuàng)建ViewGroup實(shí)例時(shí)等于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之前要明確一個(gè)概念MeasureSpec(測量規(guī)格),MeasureSpec是一個(gè)大小跟模式的組合值,控件自身的大小是受到MeasureSpec下的mode,size兩個(gè)值共同來決定的

  @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);
    }

就是在子控件正要放置自己的位置時(shí),父控件會(huì)通過MeasureSpec這個(gè)對象來問子控件,“你想要用多大地方啊?”;然后子控件通過一系列的操作來放置控件!

MeasureSpec的mode一共有三種模式:

UPSPECIFIED : 父容器對于子容器沒有任何限制,子容器想要多大就多大
EXACTLY: 父容器已經(jīng)為子容器設(shè)置了尺寸,子容器應(yīng)當(dāng)服從這些邊界,不論子容器想要多大的空間。
AT_MOST:子容器可以是聲明大小內(nèi)的任意大小

根據(jù)mode,size 共同決定的子控件最終的大?。?/p>

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

父View的MeasureSpec 是AT_MOST,說明父View的大小是不確定,最大的大小是MeasureSpec 的size值,不能超過這個(gè)值。

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

503290-20151119171702515-1341567594.png

好了 這是大概的流程,下面來個(gè)具體的示例:
布局如下:

<?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

整個(gè)圖是一個(gè)DecorView,DecorView可以理解成整個(gè)頁面的View,DecorView是一個(gè)FrameLayout,包含兩個(gè)子View,一個(gè)id=statusBarBackground的View和一個(gè)是LineaLayout,id=statusBarBackground的View,而這個(gè)LinearLayout比較重要,它包含一個(gè)title和一個(gè)content,title很好理解其實(shí)就是TitleBar或者ActionBar,content 就更簡單了,setContentView()方法你應(yīng)該用過吧,android.R.id.content 你應(yīng)該聽過吧,沒錯(cuò)就是它,content是一個(gè)FrameLayout,你寫的頁面布局通過setContentView加進(jìn)來就成了content的直接子View。

966283-4096801e91e2eccc.png

繪制的流程是這樣、詳情請參考
http://www.itdecent.cn/p/5a71014e7b1b

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容