Android custom view: An Example

Source: Internet
Author: User
Tags drawtext getcolor xml attribute

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?
  1. Package demo. View. My;
  2. Import Android. content. context;
  3. Import Android. content. res. typedarray;
  4. Import Android. Graphics. Canvas;
  5. Import Android. Graphics. color;
  6. Import Android. Graphics. paint;
  7. Import Android. Graphics. Paint. style;
  8. Import Android. util. attributeset;
  9. Import Android. View. view;
  10. /**
  11. * This is a custom textview.
  12. * At least the constructor and ondraw methods must be reloaded.
  13. * If custom views do not have their own unique attributes, they can be directly used in XML files.
  14. * If you have unique attributes, You need to obtain the custom attribute name in the attrs. XML Attribute file in the constructor.
  15. * Set the default value as needed, which is not defined in the XML file.
  16. * If you use custom attributes, you need to add a new Schemas to the application XML file,
  17. * For example, here is xmlns: My = "http://schemas.android.com/apk/res/demo.view.my"
  18. * "My" after xmlns is the prefix of the custom attribute, and "res" is the package where the custom view is located.
  19. * @ Author Administrator
  20. *
  21. */
  22. Public class myview extends view {
  23. Paint mpaint; // paint brush, which contains the style and color information of the painting ry, text, etc.
  24. Public myview (context ){
  25. Super (context );
  26. }
  27. Public myview (context, attributeset attrs ){
  28. Super (context, attrs );
  29. Mpaint = new paint ();
  30. // Typedarray is an array used to store attributes obtained by context. obtainstyledattributes.
  31. // Call the recycle method after use.
  32. // The attribute name is the name + "_" + attribute name in styleable
  33. Typedarray array = context. obtainstyledattributes (attrs, R. styleable. myview );
  34. Int textcolor = array. getcolor (R. styleable. myview_textcolor, 0xff00ff00); // provides the default value, which is not specified
  35. Float textsize = array. getdimension (R. styleable. myview_textsize, 36 );
  36. Mpaint. setcolor (textcolor );
  37. Mpaint. settextsize (textsize );
  38. Array. Recycle (); // It must be called. Otherwise, this setting will affect the next use.
  39. }
  40. Public void ondraw (canvas ){
  41. Super. ondraw (canvas );
  42. // Canvas contains many drawing interfaces. Using these interfaces, we can draw the image we want.
  43. // Mpaint = new paint ();
  44. // Mpaint. setcolor (color. Red );
  45. Mpaint. setstyle (style. Fill); // set Filling
  46. Canvas. drawrect (10, 10,100,100, mpaint); // draw a rectangle
  47. Mpaint. setcolor (color. Blue );
  48. Canvas. drawtext ("I am drawn", 10,120, mpaint );
  49. }
  50. }

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?
  1. <? XMLVersion = "1.0" encoding = "UTF-8"?>
  2. <Resources>
  3. <Declare-styleableName = "myview">
  4. <ATTRName = "textcolor" format = "color"/>
  5. <ATTRName = "textsize" format = "dimension"/>
  6. </Declare-styleable>
  7. </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?
  1. <? XMLVersion = "1.0" encoding = "UTF-8"?>
  2. <LinearlayoutXmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Xmlns: My = "http://schemas.android.com/apk/res/demo.view.my"
  4. Android: Orientation = "vertical"
  5. Android: layout_width = "fill_parent"
  6. Android: layout_height = "fill_parent"
  7. >
  8. <Demo. View. My. myview
  9. Android: layout_width = "fill_parent"
  10. Android: layout_height = "wrap_content"
  11. My: textcolor = "# ffffffff"
  12. My: textsize = "22dp"
  13. />
  14. </Linearlayout>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.