Android自定义View之onLayout源码分析.doc

Android自定义View之onLayout源码分析.doc

  1. 1、本文档共10页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Android自定义View之onLayout源码分析

Android自定义View之onLayout源码分析 我们都知道,自定义ViewGroup是所有子View的父控件,而自定义View通常需要重写onMeasure(),onLayout(),onTouchEvent()等方法,当然了,我们都知道自定义最难的地方在于draw(即画)的过程,这难以理解,不过今天这一篇文章要说的不是draw,而是如何了解onLayout()方法。我们都知道,自定义View的第一步是测量当前剩余空间,或者说是界面的大小,也就是measure了,这一点在上一篇通过讲解onMeasure()方法已经向大家解释了,所以今天我们要说的就是onLayout()方法的重写和分析了,也就是确定自定义View显示的位置。 android.view.View.layout() 既然要讲layout,确定子View在父控件的位置,那么我们就从View的layout()方法开始着手吧! 先来看一下Google官方文档对android.view.View.layout()这个方法的描述吧: Assign a size and position to a view and all of its descendants 也就是说这个方法的作用是为视图及其所有后代分配大小和位置。 This is the second phase of the layout mechanism. (The first is measuring). In this phase, each parent calls layout on all of its children to position them. This is typically done using the child measurements that were stored in the measure pass(). 这是布局机制的第二阶段。 (第一个是测量measure)。在这个阶段,每个父级调用所有子级的布局来定位它们。这通常使用存储在度量pass()中的子测量来完成。 Derived classes should not override this method. Derived classes with children should override onLayout. In that method, they should call layout on each of their children. 派生类不应覆盖此方法。具有子代的派生类应该覆盖onLayout()方法。在onLayout()方法方法中,他们应该在每个子View上调用layout()方法。 好了,以上都是通过官方文档而得知的内容,接下来我们直接看layout()方法的源码: //参数l, t, r, b分别表示子View相对于父View的左、上、右、下的坐标 public void layout(int l, int t, int r, int b) { if ((mPrivateFlags3 PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT) != 0) { onMeasure(mOldWidthMeasureSpec, mOldHeightMeasureSpec); mPrivateFlags3 = ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT; } int oldL = mLeft; int oldT = mTop; int oldB = mBottom; int oldR = mRight; boolean changed = isLayoutModeOptical(mParent) ? setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b); if (changed || (mPrivateFlags PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) { onLayout(changed, l, t, r, b); mPrivateFlags = ~PFLAG_LAYOUT_REQUIRED; ListenerInfo li = mListenerInfo; if (li != null li.mOnLayoutChangeListeners

文档评论(0)

xcs88858 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:8130065136000003

1亿VIP精品文档

相关文档