Steps to customize view
When the system components provided by andoid do not meet the requirements, it is entirely possible to integrate the view to derive the custom component.
The first defines a subclass that inherits the view, and then overrides one or more of his methods.
An introduction to overriding methods
Constructor: This is the most basic way to customize the view, which is required when creating with Java code or reading from an XML file.
Onfinishinflate () This is a callback method that is called when the application loads the component from an XML layout file and uses it to construct the interface.
Onmeasure (): The Change method detects the view component and the size of the component it contains.
OnLayout (): The location where the sub-component needs to be allocated this method is called when the child is older.
Onsizechanged (): This method is called when the size of the component is changed.
OnDraw (): This method is called when drawing his content.
OnKeyDown (): This method is called when the key is pressed.
Onkeyoff (): This method is called when the key is released.
Ontrackballevent (): This method is called when a trackball occurs.
Ontouchevent (): Touch event occurs
Onwindowfocuschanged (): When the component gets lost focus
Onattachedtowindow (): Bar when a component is placed in a window
Ondetachedfromwindow (): When the component is detached from the window
Onwindowvisibilitychanged (): When the visibility of a component changes
Sample view code with a small ball following your finger
Public class viewtest extends View { Public floatCurrentX = +; Public floatCurrentY = -;PrivatePaint paint; Public viewtest(Context context) {Super(context); } Public viewtest(Context context,attributes attiattributes) {Super(context); }@Override protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas); Paint =NewPaint (); Paint.setcolor (color.red); Canvas.drawcircle (Currentx,currenty, the, paint); }@Override Public Boolean ontouchevent(Motionevent event) {CurrentX = Event.getx (); CurrentY = Event.gety (); Invalidate ();return true; }}
Android Custom View-android Learning Journey (14)