Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android View 绘制流程 #3

Open
zhangsr opened this issue Nov 27, 2015 · 0 comments
Open

Android View 绘制流程 #3

zhangsr opened this issue Nov 27, 2015 · 0 comments

Comments

@zhangsr
Copy link
Owner

zhangsr commented Nov 27, 2015

主要绘制流程如下:

measure、layout、draw的遍历如下:

measure 过程

measure() onMeasure()
SomeView 不重写,用View的默认实现 重写,用自己特定的计算方式
SomeViewGroup 不重写,用View的默认实现 重写,用自己特定的计算方式

View的measure()默认实现做了一些缓存检查之类的工作,最终还是调用了onMeasure(),下面展示onMeasure()做了什么:

onMeasure()
SomeView (1)测量实际内容(2)setMeasured()设置自己的值
SomeViewGroup (1)读取子View的layout_*,结合自己的measuredSpec生成子View的measuredSpec(2)调用子View onMeasure()(3)获取子View测量值(4)setMeasured()设置自己的值

View大小的控制是由父View、layout.xml文件、以及View本身共同完成的,父View会提供给子View参考的大小,而开发人员可以在layout.xml文件中指定View的大小,然后View本身会对最终的大小进行拍板

measure 过程的结果:所有的View、ViewGroup都调用setMeasured()设置了自己的宽高,以备 layout 过程使用。

layout 过程

layout() onLayout()
SomeView 可重写,也可用View的默认实现 不重写,View的为空实现
SomeViewGroup 不重写,用View的默认实现 重写,用自己特定的布局方式布局子View

无论是View还是ViewGroup,layout()都可以使用View的默认方式:

  1. 设置左上右下坐标
  2. 判断有无change
  3. 有的话调用onLayout()

layout 过程的结果:设置好View、ViewGroup的左上右下坐标

draw 过程

draw() onDraw()
SomeView 不重写,用View的默认实现 重写,View的为空实现
SomeViewGroup 不重写,用View的默认实现 �不重写,�无内容绘制

onDraw()原理简单,自定义View需要重点实现,以绘制出想要的内容。

draw()虽都不重写,但很重要,View的默认实现已经把所有步骤都做好了:

  1. drawBg(canvas)
  2. onDraw(canvas)
  3. dispatchDraw(canvas)
  4. onDrawScrollBars(canvas)

ViewGroup已经重写了dispatchDraw方法,实现了遍历子View绘制,所以一般自定义ViewGroup时不需要重写

以上四个步骤按序调用,完成了所有绘制。

draw 过程的结果:将图像绘制到 Canvas 上。

参考

http://blog.csdn.net/yanbober/article/details/46128379
http://blog.csdn.net/guolin_blog/article/details/16330267
http://developer.android.com/intl/zh-cn/guide/topics/ui/how-android-draws.html
http://blog.csdn.net/luoshengyang/article/details/8372924
http://www.codekk.com/blogs/detail/54cfab086c4761e5001b253f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant