Android custom View process summary and simple example

1. The technical difficulty of customizing View is:

Custom View is a comprehensive technical system, which involves View's hierarchy, event distribution mechanism and View's working principle

etc. technical details.

Second, the classification of custom View:

It is mainly divided into four categories, which can also be said to be four ways of customizing View.

1. Rewrite onDraw() method after extends View

This category is mainly used to achieve some irregular effects, that is to say, it is not convenient to achieve this effect through the combination of layouts, and it is often necessary to statically or dynamically display some irregular graphics, so it needs to be achieved by drawing. , that is, override the onDraw() method. Using this method, you need to support wrap_content yourself, and padding also needs to be handled by yourself.

2. Extends ViewGroup derives a special layout

This method is mainly used to implement custom layouts, that is, in addition to the layouts of LinearLayout, RelayiveLayout, and FramLayout, we redefine a new layout. When a certain effect looks like a combination of several Views When together, this method can be used to achieve. When using this method, it should be noted that the measurement and layout of the ViewGroup are two processes, and the measurement and layout of the child elements are processed at the same time.

3. Extends a specific View (such as TextView, Button)

This method is relatively common and is suitable for some custom Views with relatively simple effects. It is generally used to extend the functions of an existing View, such as TextView. This method does not need to support wrap_content and padding by itself.

4. Extends a specific ViewGroup (such as LinearLayout)

This method is also relatively common. When a certain effect looks like a combination of several Views, this method can be used to achieve. In this way, you do not need to handle the measurement and layout of the ViewGroup yourself.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325583637&siteId=291194637