view的测量,布局,绘制

来源:互联网 发布:win7触控板手势软件 编辑:程序博客网 时间:2024/05/17 01:11
矩形区域. 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应用

参考博客: http://blog.csdn.net/singwhatiwanna/article/details/38426471

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




来自为知笔记(Wiz)


0 0
原创粉丝点击