Laughter to life, can penetrate the fog, laugh to life, can persist in the end, laugh to life, can defuse the crisis, smile to life, can illuminate the dark.
This content: Custom view (can be used multiple times in a layout file)
First, step:
1. Custom View Properties
2. get our custom properties in the View construction method
3, rewrite onmesure (optional, generally need to rewrite, no write default call system provided )
4. rewrite OnDraw
Attention:
second step: We have rewritten 3 constructor methods, the default layout file is called two parameters of the construction method, So remember to have all the constructor methods call our three-parameter construction method, and we get custom attributes in the construction of three parameters.
step three: system to help us measure the height and width are match_parnet, so do not rewrite we can not set Measurement of height and width. When we set it to wrap_content, it will also be shown as match_parnet.
< Span style= "Color:rgb (51,51,51); font-family:arial; line-height:26px ">measurespec of the Specmode, altogether three kinds:
exactly: Generally set an explicit value or Match_parent
At_most: Indicates that the sub-layout is limited to a maximum value, Typically warp_content
UNSPECIFIED: Indicates how large a child layout wants to be, rarely used
Example:
Here is the Res/layout/activity_main.xml layout file:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " xmlns:custom=" Http://schemas.android.com/apk/res/com.example.customview1 " Android:layout_width= "Match_parent" android:layout_height= "match_parent" > < Com.view.CustomTitleView android:layout_width= "wrap_content" android:layout_height= "Wrap_content " Android:layout_centerinparent= "true" custom:titletext= "1314" custom:titletextcolor= "#ff0000" Custom:titletextsize= "50SP"/></relativelayout>
Be sure to introducexmlns:custom= "http://schemas.android.com/apk/res/com.example.customview01"Our namespace, followed by the package path, refers to the project's
Below is the Res/values/attrs.xml file: ( )
<?xml version= "1.0" encoding= "Utf-8"?><resources> <attr name= "TitleText" format= "string"/> <attr name= "titletextcolor" format= "color"/> <attr name= "titletextsize" format= "Dimension"/> <declare-styleable name= "Customtitleview" > <attr name= "TitleText"/> <attr name= " Titletextcolor "/> <attr name=" titletextsize "/> </declare-styleable></resources>
We define the font, font color, font size 3 properties, format is the value type of the property:
Altogether there are: String,color,demension,integer,enum,reference,float,boolean,fraction,flag
Here are the custom Customtitleview.java files:
/span>
public class Customtitleview extends View {//text private String mtitletext;//The color of the text private int mtitletextcolor;//the size of the text priv ate int mtitletextsize;//Controls the extent to which text is drawn when drawing the private Rect mbound;private Paint mpaint;public Customtitleview (context context) { This (context, NULL); }public Customtitleview (Context context, AttributeSet Attrs) {This (context, attrs, 0);} Get my custom style properties Public Customtitleview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle) ;//Get the custom style attributes that we defined typedarray a = Context.gettheme (). Obtainstyledattributes (Attrs,r.styleable.customtitleview, Defstyle, 0); int n = A.getindexcount (); for (int i = 0; i < n; i++) {int attr = A.getindex (i); switch (attr) {case R.styl Eable. Customtitleview_titletext:mtitletext = a.getstring (attr); Break;case r.styleable.customtitleview_titletextcolor:// The default color is set to black Mtitletextcolor = A.getcolor (attr, color.black); Break;case r.styleable.customtitleview_titletextsize:// The default setting is 16sp,typevalue can also convert sp to pxmtitletextsize = A.getdimensionpIxelsize (attr, (int) typedvalue.applydimension (typedvalue.complex_unit_sp, 16,getresources (). Getdisplaymetrics ()) ); break;}} A.recycle ();//Obtain the width and height of the drawn text Mpaint = new paint (); mpaint.settextsize (mtitletextsize);//Mpaint.setcolor (Mtitletextcolor ); mbound = new Rect (); Mpaint.gettextbounds (mtitletext, 0, Mtitletext.length (), mbound);// This custom view does not have much advantage over TextView, so add an event to the Custom view: This.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {mtitletext = Randomtext ();p ostinvalidate ();}});} Private String Randomtext () {Random random = new random (); set<integer> set = new Hashset<integer> (), while (Set.size () < 4) {int randomint = Random.nextint; set.a DD (randomint);} StringBuffer sb = new StringBuffer (); for (Integer I:set) {sb.append ("" + i);} return sb.tostring ();} protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//Super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC); int width = 0;int height = 0;//set width int specmode = Measurespec.getmode (widthMEASURESPEC); int specsize = Measurespec.getsize (widthmeasurespec); switch (specmode) {case measurespec.exactly:// Explicitly specified width = getpaddingleft () + getpaddingright () + specsize;break;case measurespec.at_most://generally warp_contentwidth = Getpaddingleft () + getpaddingright () + mbound.width (); break;} Set the height Specmode = measurespec.getmode (heightmeasurespec); specsize = Measurespec.getsize (heightmeasurespec); switch ( Specmode) {Case measurespec.exactly://explicitly specified height = getpaddingtop () + getpaddingbottom () + specsize;break;case measurespec.at_most://generally warp_contentheight = getpaddingtop () + getpaddingbottom () + mbound.height (); Setmeasureddimension (width, height);} protected void OnDraw (canvas canvas) {mpaint.setcolor (Color.yellow); canvas.drawrect (0, 0, getmeasuredwidth (), Getmeasuredheight (), mpaint); Mpaint.setcolor (Mtitletextcolor); Canvas.drawtext (Mtitletext, GetWidth ()/2- Mbound.width ()/2,getheight ()/2 + mbound.height ()/2, Mpaint);}}
We have added a click event, each time it randomly generates a random 4-bit number, you can add a bit of noise to the OnDraw and rewrite it as a captcha.
Here is the Mainactivity.java main interface file:
public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);}}
Take your time and enjoy it
Android Custom View (i)