When developers intend to derive their own UI components, they first define a subclass that inherits the view base class, and then override one or more methods of the View class. The methods that can be rewritten by users are as follows:
Constructor: the rewritable constructor is the most basic method for customizing views. When Java code creates a view instance or loads and constructs the interface according to the XML layout file, the constructor is called.
Onfinishinflate (): This is a callback method. When an application loads the component from an XML layout file and uses it to build the interface, this method is called back.
Onmeasure (INT, INT): Call this method to check the size of the view component and all its child components.
Onlayout (Boolean, Int, INT): This method is called back when the component needs to be assigned its sub-component location and size.
Onsizechanged (INT, Int, Int, INT): This method is called back when the component size is changed.
Ondraw (cancas): calls back this method when the component is about to draw its content.
Onkeydown (INT, keyevent): This method is triggered when a key is pressed.
Onkeyup (INT, keyevent): This method is triggered when a key is released.
Ontrackballevent (motionevent): This method is triggered when a trackball event occurs.
Ontouchevent (motionevent): This method is triggered when a touch screen event occurs.
Onwindowfocuschanged (Boolean): This method is triggered when the component gets or loses focus.
Ondetachedfromwindow (): This method is triggered when the component is detached from a window.
Onwindowvisibilitychanged (INT): This method is triggered when the visibility of the window containing this component changes.
To develop a custom view, you do not need to overwrite all the methods listed above. Instead, you can rewrite some of the above methods as needed.
Develop custom View