Android custom view implementation is simple
Inherit view, override constructors, ondraw, (onmeasure) and other functions.
If a custom view requires custom attributes, you must create attrs. xml under values. Define your attributes.
Add xmlns: prefix = "http://schemas.android.com/apk/res/your custom view package path" to the XML layout file that uses the custom view ".
When using custom attributes, use the prefix: attribute name, for example, my: textcolor = "# fffffff ".
Instance:
View plaincopy to clipboardprint?
- Package demo. View. My;
- Import Android. content. context;
- Import Android. content. res. typedarray;
- Import Android. Graphics. Canvas;
- Import Android. Graphics. color;
- Import Android. Graphics. paint;
- Import Android. Graphics. Paint. style;
- Import Android. util. attributeset;
- Import Android. View. view;
- /**
- * This is a custom textview.
- * At least the constructor and ondraw methods must be reloaded.
- * If custom views do not have their own unique attributes, they can be directly used in XML files.
- * If you have unique attributes, You need to obtain the custom attribute name in the attrs. XML Attribute file in the constructor.
- * Set the default value as needed, which is not defined in the XML file.
- * If you use custom attributes, you need to add a new Schemas to the application XML file,
- * For example, here is xmlns: My = "http://schemas.android.com/apk/res/demo.view.my"
- * "My" after xmlns is the prefix of the custom attribute, and "res" is the package where the custom view is located.
- * @ Author Administrator
- *
- */
- Public class myview extends view {
- Paint mpaint; // paint brush, which contains the style and color information of the painting ry, text, etc.
- Public myview (context ){
- Super (context );
- }
- Public myview (context, attributeset attrs ){
- Super (context, attrs );
- Mpaint = new paint ();
- // Typedarray is an array used to store attributes obtained by context. obtainstyledattributes.
- // Call the recycle method after use.
- // The attribute name is the name + "_" + attribute name in styleable
- Typedarray array = context. obtainstyledattributes (attrs, R. styleable. myview );
- Int textcolor = array. getcolor (R. styleable. myview_textcolor, 0xff00ff00); // provides the default value, which is not specified
- Float textsize = array. getdimension (R. styleable. myview_textsize, 36 );
- Mpaint. setcolor (textcolor );
- Mpaint. settextsize (textsize );
- Array. Recycle (); // It must be called. Otherwise, this setting will affect the next use.
- }
- Public void ondraw (canvas ){
- Super. ondraw (canvas );
- // Canvas contains many drawing interfaces. Using these interfaces, we can draw the image we want.
- // Mpaint = new paint ();
- // Mpaint. setcolor (color. Red );
- Mpaint. setstyle (style. Fill); // set Filling
- Canvas. drawrect (10, 10,100,100, mpaint); // draw a rectangle
- Mpaint. setcolor (color. Blue );
- Canvas. drawtext ("I am drawn", 10,120, mpaint );
- }
- }
Package demo. view. my; <br/> Import android. content. context; <br/> Import android. content. res. typedarray; <br/> Import android. graphics. canvas; <br/> Import android. graphics. color; <br/> Import android. graphics. paint; <br/> Import android. graphics. paint. style; <br/> Import android. util. attributeset; <br/> Import android. view. view; <br/>/** <br/> * This is a custom textview. <br/> * at least the constructor and ondraw methods must be reloaded. <br/> * for custom v If iew does not have its own unique attributes, it can be used directly in the XML file <br/> * if it contains its own unique attributes, you need to obtain the attribute file attrs In the constructor. custom attribute name in XML <br/> * and set the default value as needed, which is not defined in the XML file. <Br/> * If custom attributes are used, a new schemas must be added to the XML file of the application. <br/> * For example, xmlns: my = "http://schemas.android.com/apk/res/demo.view.my" <br/> * Where "my" after xmlns is the prefix of the custom attribute, RES is the package where the custom view is located. <br/> * @ author administrator <br/> */<br/> public class myview extends view {</P> <p> paint mpaint; // paint brush, including the style and color information of the painting ry and text <br/> Public myview (context) {<br/> super (context ); </P> <p >}</P> <p> Public myview (context, attributeset attrs) {<br/> super (context, attrs ); <br/> mpaint = new paint (); <br/> // typedarray is used to store. array of Properties Obtained by obtainstyledattributes <br/> // after use, be sure to call the recycle method <br/> // The attribute name is in styleable + "_" + attribute name <br/> typedarray array = context. obtainstyledattributes (attrs, R. styleable. myview); <br/> int textcolor = array. getcolor (R. styleable. myview_textcolor, 0xff00ff00); // provides the default value, which is not specified <br/> float textsize = array. getdimension (R. styleable. myview_textsize, 36); <br/> mpaint. setcolor (textcolor); <br/> mpaint. settextsize (textsize); </P> <p> array. recycle (); // It must be called. Otherwise, this setting will affect the next use. <br/>}</P> <p> Public void ondraw (canvas) {<br/> super. ondraw (canvas); <br/> // canvas contains many drawing interfaces, we can plot the image we want <br/> // mpaint = new paint (); <br/> // mpaint. setcolor (color. red); <br/> mpaint. setstyle (style. fill); // set the padding <br/> canvas. drawrect (10, 10,100,100, mpaint); // draw a rectangle </P> <p> mpaint. setcolor (color. blue); <br/> canvas. drawtext ("I was drawn", 10,120, mpaint); <br/>}< br/>
Corresponding property file:
View plaincopy to clipboardprint?
- <? XMLVersion = "1.0" encoding = "UTF-8"?>
- <Resources>
- <Declare-styleableName = "myview">
- <ATTRName = "textcolor" format = "color"/>
- <ATTRName = "textsize" format = "dimension"/>
- </Declare-styleable>
- </Resources>
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <declare-styleable name = "myview"> <br/> <ATTR name = "textcolor" format = "color"/> <br/> <ATTR name = "textsize" format = "dimension"/> <br/> </declare-styleable> <br/> </resources>
Use in layout files:
View plaincopy to clipboardprint?
- <? XMLVersion = "1.0" encoding = "UTF-8"?>
- <LinearlayoutXmlns: Android = "http://schemas.android.com/apk/res/android"
- Xmlns: My = "http://schemas.android.com/apk/res/demo.view.my"
- Android: Orientation = "vertical"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- >
- <Demo. View. My. myview
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- My: textcolor = "# ffffffff"
- My: textsize = "22dp"
- />
- </Linearlayout>