android-------custom controls (1)

We often deal with controls in our ordinary Android development. Sometimes the controls provided by Android may not meet the needs of the business. At this time, we need to implement some custom controls.

Custom controls can design a lot of functions and modules you want, which is an important part of development.

 

Value of custom View

  • It can achieve effects that basic controls cannot, and add color to the performance of the application;

  • In teams developing multiple applications in parallel, common interaction effects are extracted into custom controls, which facilitates reuse and reduces unnecessary duplication of effort;

  • Encapsulate the internal logic of the control in a custom View to facilitate decoupling within the application;

 

flow chart:

 

 

There are two ways to customize the control:

1. Inherit ViewGroup 

Custom ViewGroup generally uses existing components to form new components according to a specific layout method, most of which inherit from ViewGroup or various Layouts, including child Views.

For example: ViewGroup, LinearLayout, FrameLayout, RelativeLayout, etc.

 

2. Inherit View

When there is no ready-made View and you need to implement it yourself, you can use a custom View, which generally inherits from View, SurfaceView or other Views, and does not contain child Views.

For example: View, TextView, ImageView, Button, etc.

 

 

View's three core methods onMeasure, onLayout, onDraw

  • onMeasure: used to measure the size of the view;

  • onLayout: used to layout the view;

  • onDraw: used to draw the view;

 

 

These three methods are very important, the next blog will explain in detail

Guess you like

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