Reference to:http://www.linuxidc.com/linux/2014-12/110165.htm
The previous article focused on customizing the view Why to overload the Onmeasure () method (see http://www.linuxidc.com/Linux/2014-12/110164.htm), then, What methods do you need to overload or implement to customize ViewGroup?
In Android development, there are two types of custom view, a custom control (inheriting the view Class), and a custom layout container (inheriting ViewGroup). If you are a custom control, you typically need to overload two methods, one is onmeasure () to measure the size of the control, and the other is OnDraw (), which is used to draw the UI of the control. For custom layout containers, it is generally necessary to implement/reload three methods, one is Onmeasure (), the other is used to measure dimensions, one is onlayout () to layout child controls, and the other is Dispatchdraw (), which is used to draw the UI.
This paper mainly analyzes the implementation of the OnLayout () method of the custom ViewGroup.
The OnLayout () function of the ViewGroup class is an abstract type, which must be implemented by the successor, since ViewGroup is positioned as a container to hold the child control, so it is necessary to define the way in which to put it, For example, LinearLayout is stored in the horizontal or vertical order, while the relativelayout is placed in the relative position of the child control, similarly, our custom viewgroup must also give us the desired layout, and this definition through the onlayout () function to implement.
We have a more in-depth understanding of the implementation of the OnLayout () by implementing a view container of the horizontal precedence layout, the effect (the black box is a child control and the white part is a custom layout container). The layout of the container is, first, the horizontal direction of the child control, the horizontal direction can not be placed, the other row continues to be horizontally placed.
1. Custom ViewGroup Derived classes
The first step is to customize the derived class of ViewGroup, inheriting the default constructor.
Public class extends ViewGroup { public customviewgroup (context context) { super( context); } Public Customviewgroup (Context context, AttributeSet attrs) { Super(context, attrs); } Public Customviewgroup (Context context, AttributeSet Attrs, Intdefstyle) { Super(context, Attrs, Defstyle);} }
2. Overloaded Onmeasure () method
Why overload the Onmeasure () method here is not to repeat, the previous article has been said, it is important to note that the custom ViewGroup onmeasure () method, in addition to the calculation of their own size, but also need to call Measurechildren () function to calculate the dimensions of the child control.
The definition of onmeasure () is not the focus of this article, so I use the default onmeasure () definition directly here, and of course measurechildren () must be added.
@Override protected void onmeasure (intint heightmeasurespec) { Measurechildren (Widthmeasurespec, HEIGHTMEASURESPEC); Super . Onmeasure (Widthmeasurespec, Heightmeasurespec);}
3. Implement the OnLayout () method
The OnLayout () function is prototyped as follows:
@param changed This parameter indicates whether the size or position of the current ViewGroup has changed
@param left top right bottom the coordinate position of the current viewgroup relative to its parent control
protected void OnLayout (Boolean changed,int left, int top, int. right, int bottom);
Since we want to prioritize the sub-controls in the horizontal layout, first, we know what the total width is, this value can be obtained through getmeasuredwidth (), and of course the width of the child control can also be obtained through the getmeasuredwidth () of the child control object.
In this way, it is not complicated, the specific implementation code is as follows:
protected voidOnLayout (BooleanChangedintLeftintTopintRightintbottom) { intMviewgroupwidth = Getmeasuredwidth ();//The total width of the current ViewGroup intMpainterposx = left;//current drawing cursor horizontal position intMpainterposy = top;//current drawing cursor ordinate position intChildCount =Getchildcount (); for(inti = 0; i < ChildCount; i++) {View Childview=Getchildat (i); intwidth =childview.getmeasuredwidth (); intHeight =childview.getmeasuredheight (); //move to the start of the next line if there is not enough space left if(Mpainterposx + Width >mviewgroupwidth) {MPAINTERPOSX=Left ; Mpainterposy+=height; } //performing the drawing of ChildviewChildview.layout (Mpainterposx,mpainterposy,mpainterposx+width, mpainterposy+height); //record the position of the horizontal axis that is currently drawnMpainterposx + =width; } }
4. layout file test
Here we try to write a simple XML file to test our custom viewgroup, we set the background color of the view to black, so that we can easily identify.
<com.titcktick.customview.customviewgroup xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <View android:layout_width= "100DP"Android:layout_height= "100DP"Android:layout_margin= "10DP"Android:background= "@android: Color/black"/> <View android:layout_width= "100DP"Android:layout_height= "100DP"Android:layout_margin= "10DP"Android:background= "@android: Color/black"/> <View android:layout_width= "100DP"Android:layout_height= "100DP"Android:layout_margin= "10DP"Android:background= "@android: Color/black"/> <View android:layout_width= "100DP"Android:layout_height= "100DP"Android:layout_margin= "10DP"Android:background= "@android: Color/black"/> </com.titcktick.customview.CustomViewGroup>
5. Add Layout_margin
In order to make the core logic clearer, the above onlayout () implementation I have hidden the calculation of margin, which will result in the child control layout_margin effect, so the effect is the child controls are arranged next to each other, there is no space between. So, let's look at how to add the margin effect below.
In fact, if you want to customize the ViewGroup support for the Layout_margin parameter of a child control, the custom ViewGroup class must overload the Generatelayoutparams () function, and returns a Viewgroup.marginlayoutparams derived class object in the function in order to use the margin parameter.
The key part of Viewgroup.marginlayoutparams's definition is as follows, which records the Layout_margin value of the child control:
Public Static class extends Viewgroup.layoutparams { publicint leftMargin; Public int TopMargin; Public int RightMargin; Public int BottomMargin;}
You can follow the source code to see, in fact, the XML file view of the layout_xxx parameters are passed to a variety of custom Viewgroup.layoutparams derived class objects. For example, the key part of LinearLayout's layoutparams definition is as follows:
Public classLinearLayoutextendsViewGroup { Public Static classLayoutparamsextendsViewgroup.marginlayoutparams { Public floatweight; Public intGravity =-1; PublicLayoutparams (Context C, AttributeSet attrs) {Super(c, attrs); TypedArray a=c.obtainstyledattributes (Attrs, com.android.internal.r.styleable.linearlayout_layout); Weight= A.getfloat (com.android.internal.r.styleable.linearlayout_layout_layout_weight, 0); Gravity= A.getint (com.android.internal.r.styleable.linearlayout_layout_layout_gravity,-1); A.recycle (); }} @Override Publiclayoutparams generatelayoutparams (AttributeSet attrs) {return NewLinearlayout.layoutparams (GetContext (), attrs); }}
So you can probably understand why LinearLayout's child controls support weight and gravity settings, and of course we can customize some of the params that belong to our viewgroup, which is not discussed in detail here. We only inherit marginlayoutparams to get the margin value of the child control.
Public classCustomviewgroupextendsViewGroup { Public Static classLayoutparamsextendsViewgroup.marginlayoutparams { PublicLayoutparams (Context C, AttributeSet attrs) {Super(c, attrs); }} @Override Publiclayoutparams generatelayoutparams (AttributeSet attrs) {return NewCustomviewgroup.layoutparams (GetContext (), attrs); } }
With this modification, we can get the Layout_margin value of the child control in the OnLayout () function, with the addition of the Layout_margin OnLayout () function implemented as follows:
@Overrideprotected voidOnLayout (BooleanChangedintLeftintTopintRightintbottom) { intMviewgroupwidth = Getmeasuredwidth ();//The total width of the current ViewGroup intMviewgroupheight = Getmeasuredheight ();//The total height of the current ViewGroup intMpainterposx = left;//current drawing cursor horizontal position intMpainterposy = top;//current drawing cursor ordinate position intChildCount =Getchildcount (); for(inti = 0; i < ChildCount; i++) {View Childview=Getchildat (i); intwidth =childview.getmeasuredwidth (); intHeight =childview.getmeasuredheight (); Customviewgroup.layoutparams margins=( customviewgroup.layoutparams) (Childview.getlayoutparams ()); //Childview Occupied width = Width+leftmargin+rightmargin//Childview occupied by height = Height+topmargin+bottommargin//move to the start of the next line if there is not enough space left if(mpainterposx + width + margins.leftmargin + margins.rightmargin >mviewgroupwidth) {MPAINTERPOSX=Left ; Mpainterposy+ = height + Margins.topmargin +Margins.bottommargin; } //performing the drawing of ChildviewChildview.layout (Mpainterposx+margins.leftmargin, mpainterposy+margins.topmargin,mpainterposx+ Margins.leftmargin+width, mpainterposy+margins.topmargin+height); Mpainterposx+ = width + margins.leftmargin +Margins.rightmargin; } }
6. Summary
A great effort, and finally a custom ViewGroup onlayout () related knowledge points clearly, if there are any questions welcome message or letter [email protected] exchange.
[Android Pro] Android Development Practice: OnLayout () Analysis of custom ViewGroup