How Android Draws Views

How Android Draws Views(

http://developer.android.com/guide/topics/ui/how-android-draws.html

当一个activity获取到焦点之后,他就会被要求重新绘制他的布局。Android framework 将会接受并处理重新绘制,但是activity必须提供其布局层次结构的根节点。


绘图从root节点开始。他需要测量和绘制布局树。绘画是由遍历树和呈现交叉无效区域的每个视图。反过来,每个ViewGroup负责要求他们的每个孩子被绘制(通过draw()方法),每个View被要求绘制他们自己本身。因为tree被顺序遍历,这意味着父母被绘制之后于(或,之前于)他们的孩子,有兄弟姐妹在树上画它们出现的顺序


绘制布局是两个pass的过程:一个测量传递,一个布局传递。测量传递通过measure(int,int)实现,是个从View tree的自顶向上的遍历。每个视图将尺寸规格在递归树中传递。在测量传递的最后,每个View都会保持自己的测量值。第二个传递,发生在layout(int, int, int, int)中,也是自上而下传递的。在这个传递中,每个父视图都有责任用measure传递中测量后的尺寸,将他们的子视图放在适当的位置。


当一个View对象的measure()方法返回时,他的getMeasuredWidth()和getMeasuredHeight()方法的值必须被设置,该View的全部子节点也要一同被设置。一个视图对象的测量宽度和高度测量值必须尊重其父视图的限制。这可以保证的措施通过,所有的父母都接受所有孩子的测量值。父视图可以在子View上不止一次地调用measure()方法。举个例子,


The measure pass uses two classes to communicate dimensions. The ViewGroup.LayoutParams class is used by View objects to tell their parents how they want to be measured and positioned. The base ViewGroup.LayoutParams class just describes how big the Viewwants to be for both width and height. For each dimension, it can specify one of:

  • an exact number
  • MATCH_PARENT, which means the View wants to be as big as its parent (minus padding)
  • WRAP_CONTENT, which means that the View wants to be just big enough to enclose its content (plus padding).

There are subclasses of ViewGroup.LayoutParams for different subclasses of ViewGroup. For example,RelativeLayout has its own subclass of ViewGroup.LayoutParams, which includes the ability to center child Viewobjects horizontally and vertically.

MeasureSpec objects are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:

  • UNSPECIFIED: This is used by a parent to determine the desired dimension of a child View. For example, aLinearLayout may call measure() on its child with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how tall the child View wants to be given a width of 240 pixels.
  • EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size.
  • AT MOST: This is used by the parent to impose a maximum size on the child. The child must guarantee that it and all of its descendants will fit within this size.


View


android.view.View

extends Object
implements Drawable.Callback KeyEvent.Callback AccessibilityEventSource

java.lang.Object
   ↳ android.view.View
Known Direct Subclasses
Known Indirect Subclasses //一些子类

Class Overview


这个类代表了用户界面组建的基本构建模块。一个View占据了屏幕上的一个矩形区域,并负责绘图和时间处理。View是窗口小部件的基本类,用来创建交互界面组件(如俺就,文本输入框等等)。ViewGroup是layout的基本类,它是一个无形的容器包含其他的View或者ViewGroup,并且定义他们的layout properities。

Developer Guides

For information about using this class to develop your application's user interface, read the User Interface developer guide.

Using Views


在window中所有的View被安排在同一树中。你可以通过code添加view,或者在一个或多个xml文件中指定View的tree

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值