1. Set the current View custom property to create a new Attrs file under the Values folder in Res, set the name of the custom property in the Attrs file by setting the relevant custom property
<!--The Format property can be used to restrict what types of properties are currently custom, and can be connected in the middle by |, indicating that multiple types can be supported --<attr name="User_defined_attribute" format="Color" /> <!--can specify enumeration types and customize some properties that require user selection, such as the Android:visibility property in the Android system--<attr name="user_defined_attribute1" format="enum" > <enum name="Type_0" value="0" /> <enum name="Type_1" value="1" /></attr>
Define the declare-styleable that these attributes belong to
name="MyViewStyleable"> name"user_defined_attribute"/> name"user_defined_attribute1"/></declare-styleable>
2, inherit the relevant view, in the construction method to get the related properties
Custom view usually takes precedence over the system if there is no suitable view to use, if any, should inherit the existing view of the system to extend, if the system does not have a similar view in the custom corresponding view
PublicCustomvolumcontrolbar (context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); TypedArray a = Context.gettheme (). Obtainstyledattributes (Attrs, r.styleable. Myviewstyleable, Defstyleattr,0);intn = a.getindexcount (); for(inti =0; I < n; i++) {int Index= A.getindex (i);Switch(Index) { Caser.styleable. Myviewstyleable_user_defined_attribute:mattribute = A.getint (Index, -); Break; CaseR.styleable. myviewstyleable_user_defined_attribute1:mattribute1= A.getint (Index,0); Break; }} a.recycle (); }
At the same time, the construction method will also be responsible for the initialization of some brushes and other work.
3. Assigning a value to a property in the Onmeasure (or Onsizechange) method
This is typically used to determine the width of the current view, and to calculate some of the default values for the coordinates based on the width higher.
Usually, here are some of the following code:
//Get the current mode/** * Measurespec encapsulates the layout requirements that the parent layout passes to the child layout, each Measurespec represents a set of requirements for width and height * Measurespec consists of size and mode. * Three modes: * 1.UNSPECIFIED * The parent does not have any constraints on the child, which can be any size (that is, unspecified) * When the width is not set, the pattern is UNSPECIFIED * 2.EXACTLY * The exact size of the parent, the child is limited to the given boundary , ignoring the size you want. * (when setting width or height to match_parent, the mode is exactly, because * child view occupies the space of the remaining container, so it is determined by size) * 3.at_most * Sub Maximum can reach the specified size * (When set to Wrap_cont ENT, the mode is At_most, indicating the size of the child view is the maximum, * so the child view will be based on this limit to set their own size) * * measurespecs using binary to reduce the allocation of objects. */ //Can be simply understood as://wrap_parent-Measurespec.at_most//match_parent-measurespec.exactly//Concrete value-measurespec.exactlyintHeightmode = Measurespec.getmode (Heightmeasurespec);//Get the current heightintHeightsize= measurespec.getsize (HEIGHTMEASURESPEC);//Create an appropriate value based on the size and pattern of the values passedHeightmeasurespec = Measurespec.makemeasurespec (Exceptheight, measurespec.exactly);//Reset Width heightSetmeasureddimension (Wght, wght);
4. Draw the relevant custom view in the OnDraw method
Here you need to get to the current canvas, and then use the corresponding brush for related drawing operations. It usually involves the following methods
//用来保存Canvas的状态save之后,可以调用Canvas的平移、放缩、旋转、错切、裁剪等操作canvas.save();//用来恢复Canvas之前保存的状态。防止save后对Canvas执行的操作对后续的绘制有影响canvas.restore();//save和restore要配对使用(restore可以比save少,但不能多),//如果restore调用次数比save多,会引发Error//上面两个方法结合使用可以相当于把画布分成多个图层进行绘制,各个图层间互不影响//移动坐标原点到指定位置ccanvas.translate(x,y); //对画布旋转指定的角度canvas.rotate(30);
Even the complex view is drawn again and again with paint. So as long as you slowly adjust the brush, adjust the color, a good custom view will appear.
5. Overriding the Ontouchevent method (gesture control related)
Generally used to detect some jewelry-related operations to interact with the user. The design ideas are usually as follows:
1. Get the user's current click (Touch, lift) coordinates 2. Compare a single front coordinate with a well-drawn view 3. Based on the pre-set logic, then perform the relevant operation at the corresponding location. (such as redrawing or moving, etc.) 6. Use 1 in an XML file. Full pathname reference to custom control 2. Introduce custom properties in custom controls
As shown in the following code:
<com.test.myview.view.MyTestView <!-- hwy为引用时使用的名字,可以随意,res后面为AndroidManifest中的包名, 这行引用也可以在根布局中引入 --> xmlns:hwy="http://schemas.android.com/apk/res/com.test.myview" android:layout_width="match_parent" android:layout_height="match_parent" hwy: user_defined_attribute="#ffffffff" hwy: user_defined_attribute1="type_1"/>
General steps for Android Custom view