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群英传》第3章 Android控件架构与自定义控件详解 #8

Open
panyz opened this issue Jun 2, 2017 · 0 comments
Assignees

Comments

@panyz
Copy link
Owner

panyz commented Jun 2, 2017

前言

此笔记摘抄于《Android群英传》

  • UI界面架构图:

Activity -> PhoneWindow -> DecorView -> TitleView、ContentView

  • View的测量

    • MeasureSpec是一个32位的int值,其中高2位为测量模式,低30位为测量大小,在计算中使用
      位运算的原因是为了提高并优化效率

    • 测量模式:EXACTLY(精确值模式)、AT_MOST(最大值模式)、UNSPECIFIED(不指定其大小模式)

    • View类默认的onMeasure()方法只支持EXACTLY模式,如果要让自定义View支持
      wrap_content属性,那么就要重写onMeasure()方法来指定wrap_content的大小

    • 自定义测量值的步骤:

    1. 从MeasureSpec的对象中取出具体的测量模式和大小

    2. 通过判断测量模式,给出不同的测量值,如果指定wrap_content属性,则需要
      取出指定的大小与specSize中最小的一个作为最后的测量值,例:Math.min(200,specSize);

  • View的绘制

Canvas canvas = new Canvas(bitmap);
这里的bitmap用来存储所有绘制在canvas上的像素信息

  • ViewGroup的测量

    • 当ViewGroup的大小为wrap_content时,ViewGroup就需要对子View进行遍历,以便获取
      所有子View的大小,从而决定自己的大小,而在其他的测量模式下则会通过具体的指定值来设置自身的大小

    • ViewGroup会使用dispatchDraw()方法来绘制子View

    • onFinishInflate():从xml加载组件后回调

@panyz panyz self-assigned this Jun 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant