《Expert Android》关键点摘录之一:Exploring Custom Views

来源:互联网 发布:linux修复系统 编辑:程序博客网 时间:2024/05/19 09:03

一、In Android you can customize views in three ways:

1、Custom views (by extending the View class);

2、Compound views/controls(by composing other controls through extending one of existing Layout classes);

3、Custom layouts( by extending the ViewGroup class);

 

二、the custom view

1、how to embed the costom view in any Android layouts;

2、how the cusotm view responds to touch events;

3、how the custom view remembers states as you rotate the device;

4、how to use custom attributes in layout files to initialize the custom view.

 

三、the key classes

View、ViewParent(interface)、ViewGroup(extends View and implements ViewParent)、ViewRoot

1、View

     View is the fundamental class that all of the visible compontents in Android are derived from. It defines a number of callbacks to customize its behavior, like the ability to define size, draw, and save to state.

2、ViewParent(interface)

     A ViewParent defines the protocol for any object(including another view) that wants to play the role of a parent to other views. There are two important view parents. Of course, ViewGroup is the key one. In addition to being a ViewParent, a ViewGroup also defines the protocol for a coolection of child views. All layouts like the FrameLayout and LinearLayout in Android SDK extend this class ViewGroup. ViewGroup plays a central role in defining these layouts in XML files and in placing the controls(views) at the right place. A ViewGroup also controls the background and animation of its child views.

    3、 The other key ViewParent,  the ViewRoot is implementation centric and its not a public API. In some release it is called ViewRoot, and in some implementations it is called ViewRoot Implementation and it may be changed in the future to something else. However, this class is important for understanding how drawing is done in Android.

     Being a root parent of all views in the activity, the ViewRoot schedules traversals of all the views in order to first lay them out at the right place with the right size; this is called the layout phase. The ViewRoot then traverses the view hierarchy to draw them; this phase is called drawing phase.

 

四、Layout Phase:Measurement and Layout

      The goal of the layout phase is to know the position and size of each view in the view hierarchy owned by a parent such as the VIewRoot. To calculate the position and size of each view, the ViewRoot initiates a layout phase. However, in the layout phase, the view root dose a traversal of only those views that reported or requested a layout change. This conditional measurement is to save resource and improve response time.

     The trigger to the layout phase may come from multiple events. One trigger may be the very first time everytihing is being drawn. Or one of the views, while reacting to an event like a click or touch, could report that its size has changed. In such an event, the view that got clicked on calls the method requestLayout(). This call walks up the chain and gets to the root view(ViewRoot). The root view then schedules a layout traversal message ont the main thread's queue.

 

 

 

 

原创粉丝点击