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 中, 这个方法是个抽象方法, 子类必需要实现.




相关文章
相关标签/搜索