Android View 源码解析(一) ———-民主万岁 ———-共和万岁 这是我的第一篇博客,我其实也是不知道怎样写博客,或者写什么样的博客。 既然啥都不知道我就当个技术翻译,从Android源码入手,开始分析View. ———- 开始: > *
* 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 * {@link android.view.ViewGroup} subclass is the base class for layouts, which * are invisible containers that hold other Views (or other ViewGroups) and define * their layout properties. *
这是view类开始的介绍,我帮大家翻译一下,这段英文时期已经很言简意赅的介绍了View类。 首现说一下,我的英文就是个菜鸡,所以我就用百度翻译给大家介绍一下。注意一下:括号外面的是百度翻译,括号里面的是我的翻译,如果一句话没有带括号就说明我的意思跟百度翻译的是一样的。 <p> 这个类表示用户界面组件的基本构建块(表示所有的界面组件都是view)。视图在屏幕上占据一个矩形区域,负责绘图和事件处理。View是用于创建交互式UI组件(按钮、文本字段等)的“组件”的基类。这个{@链接Android。观。ViewGroup }类是< EM >布局< / EM >的基类,它是看不见的容器持有其他观点(或其他viewgroup)并确定其布局属性。(ViewGroup 是所有布局的基类),它是看不见的容器持有View或者ViewGroup 并定义它们的布局属性。
这就是google 大神对View类的说明。
* <div class="special reference">
* <h3>Developer Guides</h3>
* <p>For information about using this class to develop your application's user interface,
* read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
* </div>
有关使用此类开发应用程序用户界面的信息 可以参考guide/topics/ui/index.html这个开发文档。 大家在查东西的时候可以看到这个文档,如果里面的英文看的吃力的话,可以选择中文。文档地址见下图。![ 这个是上面文档的地址](https://img-blog.csdn.net/20170306131245551?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjg4NjU5ODk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)。
* <a name="Using"></a>
* <h3>Using Views</h3>
* <p>
* All of the views in a window are arranged in a single tree. You can add views
* either from code or by specifying a tree of views in one or more XML layout
* files. There are many specialized subclasses of views that act as controls or
* are capable of displaying text, images, or other content.
* </p>
窗口中的所有视图都被安排在一棵树中。您可以添加视图从代码或通过在一个或多个xml布局中指定视图树文件夹.有许多专门的子视图作为控件或能够显示文本、图像或其他内容。(这里大概介绍一下怎样使用View和View的树结构)
* <p>
* Once you have created a tree of views, there are typically a few types of
* common operations you may wish to perform:
* <ul>
* <li><strong>Set properties:</strong> for example setting the text of a
* {@link android.widget.TextView}. The available properties and the methods
* that set them will vary among the different subclasses of views. Note that
* properties that are known at build time can be set in the XML layout
* files.</li>
* <li><strong>Set focus:</strong> The framework will handled moving focus in
* response to user input. To force focus to a specific view, call
* {@link #requestFocus}.</li>
* <li><strong>Set up listeners:</strong> Views allow clients to set listeners
* that will be notified when something interesting happens to the view. For
* example, all views will let you set a listener to be notified when the view
* gains or loses focus. You can register such a listener using
* {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
* Other view subclasses offer more specialized listeners. For example, a Button
* exposes a listener to notify clients when the button is clicked.</li>
* <li><strong>Set visibility:</strong> You can hide or show views using
* {@link #setVisibility(int)}.</li>
* </ul>
* </p>
一旦你创建了一个视图树,通常有几种类型的常见的操作,你可能希望执行: 1.设置属性:例如设置{@链接Android TextView }。小部件。可用的性质和方法它们在不同的子视图中会有所不同。请注意,在生成时已知的属性可以在xml布局中设置文件夹.(根据子View的属性设置属性) 2.设置焦点将处理用户输入响应。强制聚焦到特定视图,调用{“链接# requestfocus }(设置焦点) 3.设置侦听器:视图允许客户端设置侦听器这将通知当一些有趣的事情发生的。 例如,所有视图将允许您设置侦听器在视图时通知得失焦点。您可以使用这样的监听器注册 {“链接# setonfocuschangelistener(Android。视图。视图。OnFocusChangeListener)}。 其他视图子类提供更专门的侦听器。例如,一个按钮当单击按钮时,将侦听器通知客户端通知客户端 (主要讲view可以和用户实现互动,并监听各种事件,并且将了一个例子)。 4.设置可见 setVisibility(int)
* <p><em>
* Note: The Android framework is responsible for measuring, laying out and
* drawing views. You should not call methods that perform these actions on
* views yourself unless you are actually implementing a
* {
@link android.view.ViewGroup}.
* </em></p>
Android框架负责测量、布局和绘制视图。你不应该调用方法执行这些方法的意见除非你自己实现了ViewGroup.(主要说 Android框架会自己实现 view的测量 布局和绘制)。
* <a name="Lifecycle"></a>
* <h3>Implementing a Custom View</h3>
声明周期 实现自定义View
* <p>
* To implement a custom view, you will usually begin by providing overrides for
* some of the standard methods that the framework calls on all views. You do
* not need to override all of these methods. In fact, you can start by just
* overriding {@link #onDraw(android.graphics.Canvas)}.
* <table border="2" width="85%" align="center" cellpadding="5">
* <thead>
* <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
* </thead>
*
* <tbody>
* <tr>
* <td rowspan="2">Creation</td>
* <td>Constructors</td>
* <td>There is a form of the constructor that are called when the view
* is created from code and a form that is called when the view is
* inflated from a layout file. The second form should parse and apply
* any attributes defined in the layout file.
* </td>
* </tr>
* <tr>
* <td><code>{@link #onFinishInflate()}</code></td>
* <td>Called after a view and all of its children has been inflated
* from XML.</td>
* </tr>
*
* <tr>
* <td rowspan="3">Layout</td>
* <td><code>{@link #onMeasure(int, int)}</code></td>
* <td>Called to determine the size requirements for this view and all
* of its children.
* </td>
* </tr>
* <tr>
* <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
* <td>Called when this view should assign a size and position to all
* of its children.
* </td>
* </tr>
* <tr>
* <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
* <td>Called when the size of this view has changed.
* </td>
* </tr>
*
* <tr>
* <td>Drawing</td>
* <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
* <td>Called when the view should render its content.
* </td>
* </tr>
*
* <tr>
* <td rowspan="4">Event processing</td>
* <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
* <td>Called when a new hardware key event occurs.
* </td>
* </tr>
* <tr>
* <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
* <td>Called when a hardware key up event occurs.
* </td>
* </tr>
* <tr>
* <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
* <td>Called when a trackball motion event occurs.
* </td>
* </tr>
* <tr>
* <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
* <td>Called when a touch screen motion event occurs.
* </td>
* </tr>
*
* <tr>
* <td rowspan="2">Focus</td>
* <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
* <td>Called when the view gains or loses focus.
* </td>
* </tr>
*
* <tr>
* <td><code>{@link #onWindowFocusChanged(boolean)}</code></