Simple Example of custom View: Custom view
In the development process, although the Android system provides a lot of controls for us to use, it still cannot meet our human needs and we feel we are greedy! At this time, we may need to use custom controls and Custom Attributes. How should we perform this operation?
Follow these steps:
1. inherit from View or other controls and override constructors such as onDraw, onMeasure, and onTouch.
2. For custom attributes, you need to create attrs. xml under values to define the attributes you need. For details about the attribute types, see http://www.jb51.net/article/32172.htm.
3. Add xmlns: prefix = "http://schemas.android.com/apk/res/custom view package path" to the xml layout file of the custom View. For example, the prefix I used is xmlns: app. When using custom attributes, the prefix: attribute name is also used.
The following is an example:
---------- Define View and override onDraw ()---------------
Public class DefaultView extends View {
/** Paint, use and set the color and style of the drawing pattern */
Private Paint mPaint;
/** Constructor */
Public DefaultView (Context context ){
Super (context );
}
Public DefaultView (Context context, AttributeSet attrs ){
Super (context, attrs );
MPaint = new Paint ();
/** TypedArray is used to store an array of attributes obtained by context. obtainStyledAttributes.
* After use, you must call the recycle method.
*/
TypedArray array = context. obtainStyledAttributes (attrs, R. styleable. DefaultView );
/** Obtain the color value. Otherwise, use the default value */
Int textColor = array. getColor (R. styleable. DefaultView_textColor, 0XFF00FF00 );
/** Get the text size; otherwise, use the default value */
Float textSize = array. getDimension (R. styleable. DefaultView_textSize, 36 );
MPaint. setColor (textColor );
MPaint. setTextSize (textSize );
/** If you do not call the recycle () method, this setting will affect the next use */
Array. recycle ();
}
/** Override the onDraw () method. The pattern to be drawn is implemented here */
Public void onDraw (Canvas canvas ){
Super. onDraw (canvas );
/** Paint color */
MPaint. setColor (Color. RED );
/** Paint style */
MPaint. setStyle (Style. FILL );
/** Paint width */
MPaint. setStrokeWidth (2 );
/** Draw a rectangle */
Canvas. drawRect (10, 10,100,100, mPaint );
MPaint. setColor (Color. BLUE );
/** Set the paint brush to resist the teeth */
MPaint. setAntiAlias (true );
/** Draw text */
Canvas. drawText ("Draw text", 20,120, mPaint );
MPaint. setColor (Color. GREEN );
/** Circle */
Canvas. drawCircle (100f, 100f, 50f, mPaint );
}
}
------- Create the attrs. xml file in the res/values folder and customize the attributes of our View ------------------------
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "DefaultView">
<Attr name = "textColor" format = "color"/>
<Attr name = "textSize" format = "dimension"/>
</Declare-styleable>
</Resources>
---------- Use ---------- In our layout File ----------
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Xmlns: app = "http://schemas.android.com/apk/res/com.ldm.map" <! -- Add this sentence -->
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<Com. ldm. map. DefaultView
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
App: textColor = "# f0f0f0" <! -- Prefix: app: -->
App: textSize = "16sp"/>
</LinearLayout>