view的測量,佈局,繪製

View 就是屏幕上的一塊 矩形區域. Android原生桌面上的那些應用圖標實際上是 textview, 能夠再 Android Device Monitor 中看(dump view).
View只能有一個父View, 爲何? 這樣設計, 系統底層只需跟最頂層的View交互. View是否必須有父View, 不是, 可使用 WindowManager 加到屏幕上

Android是 C/S 架構, 咱們寫的客戶端應用就是C, 系統提供的服務是S. Java中各個應用程序都只能運行在本身的內存中, 不容許跨進程訪問.
Binder - aidl(一份通訊的協議, 說明書), Android的跨進程訪問方式. Binder是Linux下的一個驅動, 驅動是用來管理硬件的, Binder是用來管理一小段內存的. 
a應用


什麼是View
在Android的官方文檔中是這樣描述的:表示了用戶界面的基本構建模塊。一個View佔用了屏幕上的一個矩形區域而且負責界面繪製和事件處理。
手機屏幕上全部看得見摸得着的都是View。這一點對全部圖形系統來講都同樣,例如ios的UIView。

一個View要想被顯示出來,須要通過3個步驟
1.要有多大的區域
measure( 判斷是否須要調用 measure, 好比緩存) --->onMeasure(由咱們重寫)--->setMeasuredDimension(使用成員變量記住寬和高)
2.肯定要畫的位置
layout--->setFrame(用成員變量記住上下左右, 而且判斷是否改變, 若是改變, 調用onSizeChanged)--->onLayout(由咱們重寫)
3.畫成什麼樣子
draw--->onDraw(由咱們重寫)--->dispatchDraw(View中是空處理, ViewGroup中會作一些事)

View System 中最頂層的View
2.3以前:
DecorView--->LinearLayout--------TextView = Fill  42dip
1 300  1 300        --------FrameLayout   FILL      FILL
 1   540    1 960-42-42
2.3以後:
DecorView--->View(確切的說是ViewGroup)--------ActionBar
                                                                       --------FrameLayout
DecorView是一個FrameLayout, 他的父親是 ViewRoot
ViewRoot -- 是一個Binder
|  1 540  1 960
|                                    1   540   1  42*1.5

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
兩個參數由父控件傳入, 表示指望此View的大小, 使用一個 int 型的值表示兩個參數,  最高兩位表示 mode, 低30位表示 size, 可使用 View. MeasureSpec 這個類獲取
        /**
         * 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;

        /**
         * 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;

        /**
         * 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;
mode 是一種基於策略的考慮, 父控件的指望值並不表明此View必須是這個值, 可是, 系統是但願能遵照這個指望.


view.getViewTreeObserver.addGlobalLayoutListener(new 


protected void onLayout(boolean changed, int left, int top, int right, int bottom)
在View中, 這個方法是個空實現, 可是在ViewGroup 中, 這個方法是個抽象方法, 子類必需要實現.




相關文章
相關標籤/搜索