#1024程序员节#
学习 Android 的 View 体系前在心里先来个灵魂三问,然后逐一分析:
- 什么是View?
- 为什么要学View?
- 该怎么学View?
对于Android的View体系学习,归根结底是View.java和ViewGroup.java,我们直接看这两个类的注释(android.view.View.java):
This class represents the basic building block for user interface components. A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). The ViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.
// 往上再没有父类,实现了三个接口
public class View implements Drawable.Callback, KeyEvent.Callback,
AccessibilityEventSource {
...// 3万多行代码,不要从头看,会晕,需要掌握方式方法来学
大致意思如下:
该类代表用户界面组件的基本构建块。 一个 View 在屏幕上占据一个矩形区域,负责绘制和事件处理。 View 是小部件的基类,用于创建交互式 UI 组件(按钮、文本字段等)。 ViewGroup 子类是布局的基类,布局是包含其他 View(或其他 ViewGroup)并定义其布局属性的不可见容器。
再看ViewGroup.java(android.view.ViewGroup.java)
A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers. This class also defines the ViewGroup.LayoutParams class which serves as the base class for layouts parameters.
// 继承自View并实现了两个接口
public abstract class ViewGroup extends View implements ViewParent, ViewManager {
...// 9千多行代码
ViewGroup的注释为:
ViewGroup 是一个特殊的视图,可以包含其他视图(称为子视图)。视图组是布局和视图容器的基类。 该类还定义了ViewGroup.LayoutParams
类,该类用作布局参数的基类。
有了是什么,就有了一个初步的认识,接下来思考为什么要学?
- 能更好的掌握系统控件的使用;
- 实现系统未提供的自定义控件;
- 解决项目中遇到的实际问题;
- 。。。
关于如何学习,我这里初步做了个提纲,近期会逐一填补:
- View的绘制流程
- 自定义View
- 事件处理机制
- 事件分发机制
- 实现PhotoView(自定义控件+事件处理)
- 常用且重要的系统控件拆解
- …
学习完控件View的内容,最后再补充一个动画。