1. Overview
It's a treasure trove of GitHub to watch and learn more.
This project is mainly to achieve the digital progress bar effect
GitHub address in Https://github.com/daimajia/NumberProgressBar
Thanks to the open source author!
Comb the main points of knowledge:
"1" familiar with the process of customizing view
"2" Implementation principle
The view coordinate system in "3" Android uses
"4" onmeasure elegant method of writing
DrawText method in "5" Canvas notice points
The "6" code is very readable
2. Project Essentials Analysis
"Familiar with the process of customizing View"
Custom view you need to read a lot of wonderful code, but the process is basically consistent in my custom view introduction in detail in this way to analyze the custom view
"The realization principle of this project"
The project is relatively basic and suitable as an introductory learning project, and the author mainly divides the custom controls into 3 large areas
Mreachedrectf--text area (optionally not available)--MUNREACHEDRECTF
(The control supports no text area), mainly by controlling the coordinates of the MREACHEDRECTF and MUNREACHEDRECTF to continuously refresh the UI to achieve a moving effect, without using the animation
Customizing the View step Get custom properties
Here the author writes directly to one of the constructor methods
Public Numberprogressbar (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, Defstylea TTR); Default_reached_bar_height = dp2px (1.5f); Default_unreached_bar_height = dp2px (1.0f); Default_text_size = sp2px (10); Default_progress_text_offset = dp2px (3.0f); Load styled attributes. Final TypedArray attributes = Context.gettheme (). Obtainstyledattributes (Attrs, R.styleable.numberprogressbar, defstyleattr, 0); Mreachedbarcolor = Attributes.getcolor (R.styleable.numberprogressbar_progress_reached_color, Default_reached_ color); Munreachedbarcolor = Attributes.getcolor (R.styleable.numberprogressbar_progress_unreached_color, Default_ Unreached_color); Mtextcolor = Attributes.getcolor (R.styleable.numberprogressbar_progress_text_color, Default_text_color); Mtextsize = Attributes.getdimension (r.styleable.numberprogressbar_progress_text_size, default_text_size); MreachedbarheiGht = Attributes.getdimension (R.styleable.numberprogressbar_progress_reached_bar_height, Default_reached_bar_ height); Munreachedbarheight = Attributes.getdimension (R.styleable.numberprogressbar_progress_unreached_bar_height, Default_unreached_bar_height); Moffset = Attributes.getdimension (R.styleable.numberprogressbar_progress_text_offset, Default_progress_text_ Offset); int textvisible = Attributes.getint (r.styleable.numberprogressbar_progress_text_visibility, PROGRESS_TEXT_VISIBLE) ; if (textvisible! = progress_text_visible) {Mifdrawtext = false; } setprogress (Attributes.getint (r.styleable.numberprogressbar_progress_current, 0)); Setmax (Attributes.getint (R.styleable.numberprogressbar_progress_max, 100)); Attributes.recycle (); Initializepainters (); }
Here the author begins to initialize the custom properties, and usually we can use a function alone under each constructor
Then there is onmeasure, which is more elegant
Which exactly mainly for match_parent/specific parameters
Atmost mainly for the wrap_content situation here to do the processing, sometimes if we put the layout file width and height written wrap_content, if the parent layout is at_most at this point is the parent layout praentsize
Therefore we support setting wrap_content time need to rewrite the Onmeasure method, the following is also done processing (at_most)
This is also illustrated by the Custom View blog of the Grand God http://blog.csdn.net/lmj623565791/article/details/24252901
And unspecified is often used for internal measurement of the system, usually only need to focus on atmost and exactly
@Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {setmeasureddimension (Measure (w Idthmeasurespec, True), measure (Heightmeasurespec, false)); } private int measure (int measurespec, Boolean iswidth) {int result; int mode = Measurespec.getmode (MEASURESPEC); int size = Measurespec.getsize (MEASURESPEC); int padding = iswidth? Getpaddingleft () + getpaddingright (): Getpaddingtop () + Getpaddingbottom (); if (mode = = measurespec.exactly) {result = size; } else {result = Iswidth? Getsuggestedminimumwidth (): Getsuggestedminimumheight (); result + = padding; if (mode = = Measurespec.at_most) {if (iswidth) {result = Math.max (result, size); } else {result = Math.min (result, size); }}} return result; }
The Ondrawer method is 6666 more readable
Sometimes we also have to pay attention to writing "code that can be spoken," noting the encapsulation of functions
@Override protected void OnDraw (canvas canvas) { if (mifdrawtext) { CALCULATEDRAWRECTF (); } else { Calculatedrawrectfwithoutprogresstext (); } if (Mdrawreachedbar) { canvas.drawrect (MREACHEDRECTF, mreachedbarpaint); } if (Mdrawunreachedbar) { canvas.drawrect (MUNREACHEDRECTF, munreachedbarpaint); } if (mifdrawtext) canvas.drawtext (Mcurrentdrawtext, Mdrawtextstart, Mdrawtextend, mtextpaint); }
This code we can clearly see the author's logic if you set a digital display executionCALCULATEDRAWRECTF() otherwise executedCalculatedrawrectfwithoutprogresstext()
The two functions are to begin to deal with the position changes of the MREACHEDRECTF and MUNREACHEDRECTF rectangles mentioned earlier, and you need to familiarize yourself with the coordinate system in Android and the access to the click location.
Look at the text when the two areas of the calculation of the main focus on the beginning and final stage of the text unknown special circumstances
private void Calculatedrawrectf () {mcurrentdrawtext = String.Format ("%d", getprogress () * 100/getmax ()); Mcurrentdrawtext = mprefix + mcurrentdrawtext + msuffix;//converted to string mdrawtextwidth = Mtextpaint.measuretext (MCurr Entdrawtext);//measure the length of the text if (getprogress () = = 0) {Mdrawreachedbar = false; Mdrawtextstart = Getpaddingleft ();//Start position (right)} else {Mdrawreachedbar = true; Mreachedrectf.left = Getpaddingleft (); Mreachedrectf.top = GetHeight ()/2.0f-mreachedbarheight/2.0f; Mreachedrectf.right = (getwidth ()-Getpaddingleft ()-getpaddingright ())/(Getmax () * 1.0f) * getprogress ()-Moffset + Getpaddingleft (); Mreachedrectf.bottom = GetHeight ()/2.0f + mreachedbarheight/2.0f; Mdrawtextstart = (mreachedrectf.right + moffset);//actual Right position}//mtextpaint.descent () + mtextpaint.ascent () bas Eline to font maximum +baseline to font minimum = actual font height mdrawtextend = (int) ((getHeight ()/2.0f)-((Mtextpaint.descent () + mtextpaint.ascent ())/2.0f)); if (Mdrawtextstart + mdrawtextwidth) >= getwidth ()-getpaddingright ()) {///text end position reset text start position and MREACHEDRECTF rectangle's Right Mdrawtextstart = getwidth ()-Getpaddingright ()-mdrawtextwidth; Mreachedrectf.right = Mdrawtextstart-moffset; } Float Unreachedbarstart = Mdrawtextstart + mdrawtextwidth + moffset; if (Unreachedbarstart >= getwidth ()-getpaddingright ()) {//not to the end mdrawunreachedbar = false; } else {Mdrawunreachedbar = true; Munreachedrectf.left = Unreachedbarstart; Munreachedrectf.top = GetHeight ()/2.0f-munreachedbarheight/2.0f; Munreachedrectf.right = GetWidth ()-getpaddingright (); Munreachedrectf.bottom = GetHeight ()/2.0f + munreachedbarheight/2.0f; } }
Here the main mention of Mtextpaint.Descent ()+Mtextpaint.Ascent () The reference distance is baseline, so that you can calculate the height of the entire font, in particular, you can refer to
http://mikewang.blog.51cto.com/3826268/871765/details the font properties and measurements of the Androd
and the xy coordinate in the Canvas.drawtext method is actually the position of the baseline, which is useful when calibrating the font position!
Numberprogressbar Open Source Project Learning