The theory and flowchart of the custom controls in Android are described in the previous section, and the methods are explained in detail today by code.
First create a class circularview inherit from View, then implement the constructor method (initialization step)
Public classCircularviewextendsView { PublicCircularview (Context context) {Super(context); } PublicCircularview (Context context, AttributeSet attrs) {Super(context, attrs); } PublicCircularview (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); } PublicCircularview (context context, AttributeSet attrs,intDefstyleattr,intdefstyleres) { Super(context, Attrs, defstyleattr, defstyleres); }
The view constructor has four overloads, and the view constructor has fewer arguments.
A constructor with four parameters is added on API21 and is temporarily not considered.
The third parameter in the constructor with three arguments is the default style, where the default style is the default style in the theme that it is currently application or activity in, and only takes effect when explicitly called.
// a constructor with three parameters is called and the third parameter is explicitly specified This (Context, attrs, Com.android.internal.r.attr.imagebuttonstyle);
Since the third parameter of the constructor for three parameters is generally not commonly used, it is not considered in the first
// It is usually called when a view is directly new. publicvoid Circularview (context context) {}// is typically called when used in layout files, All of its properties, including custom attributes, are included in the attrs. publicvoid Circularview (context context, AttributeSet attrs) {}
How to use: (in a layout file)
< com . Zhangqie.customcontrol.demo1.CircularView android:layout_width= "Wrap_content" Android:layout_height= "Wrap_content" android:layout_margin= "5DP" Android:background = "@color/coloraccent" />
Onmeasure Method (Measure view size)
The size of the view is determined not only by itself, but also by the parent control, in order for our control to better adapt to a variety of situations, it will usually measure itself.
@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { intWidthsize measurespec.getsize (WIDTHMEASURESPEC);//the exact value of the removed width intWidthmode Measurespec.getmode (WIDTHMEASURESPEC);//measuring mode for removing width intHeightsize measurespec.getsize (HEIGHTMEASURESPEC);//the exact value of the removal height intHeightmode Measurespec.getmode (HEIGHTMEASURESPEC);//measuring mode for removing height}
As can be seen from the Onmeasure function in the Widthmeasurespec and heightmeasurespec the parameters of the two int, no doubt they are related to the width , but they are not width and height, but by the width, height and the respective side An upward corresponding measurement mode to synthesize a value:
A total of three measurement modes are defined in an internal class view.measurespec of the View class in Android:
| Mode | Binary
Values |
Description |
| UNSPECIFIED |
00 |
The default value, the parent control does not give the child view any restrictions, the child view can be set to any size. |
| Exactly |
01 |
Indicates that the parent control has exactly specified the size of the child view. |
| At_most |
10 |
Indicates that the child view size has no size limit, but there is an upper limit, which is usually the parent view size. |
The onsizechanged method determines the size of the view, which is called when the view size has changed.
/*** * Determines the size of the view (this function is called when the view size has changed.) * * Width, height, last width, last height. * Just focus on width (w), Height (h), and these two parameters are the final size of the view. * @paramW *@paramh *@paramOLDW *@paramOLDH*/@Overrideprotected voidOnsizechanged (intWintHintOLDW,intOLDH) { Super. Onsizechanged (W, H, OLDW, OLDH); LOG.I (TAG,"Onsizechanged"); }
OnLayout method to determine sub-view layout location
/*** * layout-layout procedure is used to set the position that the view appears on the screen, onlayout generally only used in custom ViewGroup * *@paramchanged *@paramLeft *@paramTop *@paramRight *@paramBottom*/@Overrideprotected voidOnLayout (BooleanChangedintLeftintTopintRightintbottom) { Super. OnLayout (changed, left, top, right, bottom); LOG.I (TAG,"OnLayout"); }
The function that determines the layout is onlayout, which is used to determine the location of the child view, which is used in the custom ViewGroup, and he invokes the layout function of the child view.
In custom ViewGroup, onlayout generally loops out the child view, then calculates the coordinate values for each sub-view position, and then sets the child view position with the following function.
OnDraw method, drawing content
/** * * Drawing the-draw process is primarily used to take advantage of the parameters obtained in the first two steps, to display the view on the screen, and to complete the whole view drawing work here @param canvas */ @Override protectedvoid onDraw (canvas canvas) { Super. OnDraw (canvas); }
OnDraw is the actual drawing part, which is the part we really care about, using canvas drawing.
Android--------Custom Controls (ii)