[Android UI] Share: A simple and generous custom progress bar Numberprogressbar

Source: Internet
Author: User
Tags getcolor

This is a personalized component on the Internet, I feel very concise atmosphere, so this record can also share with you this good progress bar component

As an effect:


This is a custom view, I'm not going to delve into this, look at the demo interface code to see how to use.

Mainactivity.java

/** * Demo Main interface * @author Jan */public class Mainactivity extends Activity implements Onprogre Ssbarlistener {Private Timer timer;//progress bar component Private Numberprogressbar BNP; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); BNP = ( Numberprogressbar) Findviewbyid (R.ID.NUMBERBAR1) Bnp.setonprogressbarlistener (this);//Create a timed task, simulate progress update timer = new Timer (); Timer.schedule (new TimerTask () {@Overridepublic void run () {Runonuithread (new Runnable () {@Overridepublic void Run () {//Increment progress Value Bnp.incrementprogressby (5);}});}}, 1000, 100);} @Overrideprotected void OnDestroy () {Super.ondestroy ();//finally do not forget to cancel the scheduled task Timer.cancel ();} /** * Enables real-time monitoring of progress, max defaults to A/@Overridepublic void onprogresschange (int current, int max) {if (current = = max) {Toast.makete XT (Getapplicationcontext (), getString (r.string.finish), Toast.length_short). Show (); bnp.setprogress (0);}} 
2. Add him to our XML layout file and don't forget to define the namespace: xmlns:custom= "Http://schemas.android.com/apk/res-auto":

<com.daimajia.numberprogressbar.numberprogressbar        android:id= "@+id/numberbar1"        style= "@style/ Numberprogressbar_default "        android:layout_width=" wrap_content "        android:layout_height=" Wrap_content "        android:padding= "20DP"        custom:progress_current= "0"/>
3. His style setting has the following properties:

<style name= "Numberprogressbar_default" >        <item name= "Android:layout_height" >wrap_content</ item>        <item name= "android:layout_width" >match_parent</item>        <item name= "Progress_max" >100</item>        <item name= "progress_current" >0</item>        <item name= "Progress_ Unreached_color "> #CCCCCC </item>        <item name=" Progress_reached_color "> #3498DB </item>        <item name= "progress_text_size" >14sp</item>        <item name= "Progress_text_color" > #3498DB </item>        <item name= "progress_reached_bar_height" >6.5dp</item>        <item name= "Progress_ Unreached_bar_height ">5.75dp</item>    </style>

4. Take a look at his source code.

public class Numberprogressbar extends View {private int mmaxprogress = 100;     /** * Progress, can not exceed the max progress.    */private int mcurrentprogress = 0;     /** * The progress area bar color.    */private int mreachedbarcolor;     /** * the bar unreached area color.    */private int munreachedbarcolor;     /** * The progress text color.    */private int mtextcolor;     /** * The progress text size.    */private float mtextsize;     /** * The height of the reached area.    */private float mreachedbarheight;     /** * The height of the unreached area.    */private float munreachedbarheight;     /** * The suffix of the number.    */private String Msuffix = "%";     /** * the prefix.    */private String mprefix = "";    Private final int default_text_color = COLOR.RGB (66, 145, 241);    Private final int default_reached_color = COLOR.RGB (66, 145, 241); Private final int Default_unreached_cOlor = Color.rgb (204, 204, 204);    Private final float Default_progress_text_offset;    Private final float default_text_size;    Private final float default_reached_bar_height;    Private final float default_unreached_bar_height;     /** * For Save and restore instance of ProgressBar.    */private static final String instance_state = "Saved_instance";    private static final String Instance_text_color = "Text_color";    private static final String instance_text_size = "Text_size";    private static final String instance_reached_bar_height = "Reached_bar_height";    private static final String Instance_reached_bar_color = "Reached_bar_color";    private static final String instance_unreached_bar_height = "Unreached_bar_height";    private static final String Instance_unreached_bar_color = "Unreached_bar_color";    private static final String Instance_max = "MAX";    private static final String instance_progress = "PROGRESS"; private static final String Instance_suffix = "SuFfix ";    private static final String Instance_prefix = "PREFIX";    private static final String instance_text_visibility = "text_visibility";    private static final int progress_text_visible = 0;     /** * The width of the text that is drawn.    */private float mdrawtextwidth;     /** * The drawn text start.    */private float Mdrawtextstart;     /** * The drawn text end.    */private float mdrawtextend;     /** * The text that is drawn in OnDraw ().    */Private String mcurrentdrawtext;     /** * The Paint of the reached area.    */Private Paint mreachedbarpaint;     /** * The Paint of the unreached area.    */Private Paint munreachedbarpaint;     /** * The Paint of the progress text.    */Private Paint mtextpaint;     /** * unreached bar area to draw rect.    */private RECTF MUNREACHEDRECTF = new RECTF (0, 0, 0, 0);     /** * reached bar area rect.  */private RECTF MREACHEDRECTF = new RECTF (0, 0, 0, 0);  /** * The progress text offset.    */private float moffset;     /** * Determine if need to draw unreached area.    */Private Boolean mdrawunreachedbar = true;    Private Boolean Mdrawreachedbar = true;    Private Boolean mifdrawtext = true;    /** * Listener * * * private onprogressbarlistener Mlistener;        public enum Progresstextvisibility {Visible, Invisible} public Numberprogressbar (context context) {    This (context, NULL); } public Numberprogressbar (context context, AttributeSet Attrs) {This (context, attrs, R.attr.numberprogressbars    Tyle); } public Numberprogressbar (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, defsty        LEATTR);        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 ();    } @Override protected int getsuggestedminimumwidth () {return (int) mtextsize; } @Override protected int getsuggestedminimumheight () {return Math.max ((int) mtextsize, Math.max ((int) MREAC    Hedbarheight, (int) munreachedbarheight)); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {setmeasureddimension (meas    Ure (Widthmeasurespec, 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;        } @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);        } private void Initializepainters () {mreachedbarpaint = new Paint (Paint.anti_alias_flag);        Mreachedbarpaint.setcolor (Mreachedbarcolor);        Munreachedbarpaint = new Paint (Paint.anti_alias_flag);        Munreachedbarpaint.setcolor (Munreachedbarcolor);        Mtextpaint = new Paint (Paint.anti_alias_flag);        Mtextpaint.setcolor (Mtextcolor);    Mtextpaint.settextsize (mtextsize);        } private void Calculatedrawrectfwithoutprogresstext () {mreachedrectf.left = Getpaddingleft ();        Mreachedrectf.top = GetHeight ()/2.0f-mreachedbarheight/2.0f; Mreachedrectf.right = (getwidth ()-Getpaddingleft ()-getpaddingright ())/(Getmax () * 1.0f) * getprogress () + getpadding        Left ();        Mreachedrectf.bottom = GetHeight ()/2.0f + mreachedbarheight/2.0f; MunreacHedrectf.left = Mreachedrectf.right;        Munreachedrectf.right = GetWidth ()-getpaddingright ();        Munreachedrectf.top = GetHeight ()/2.0f +-munreachedbarheight/2.0f;    Munreachedrectf.bottom = GetHeight ()/2.0f + munreachedbarheight/2.0f;        } private void Calculatedrawrectf () {mcurrentdrawtext = String.Format ("%d", getprogress () * 100/getmax ());        Mcurrentdrawtext = mprefix + Mcurrentdrawtext + msuffix;        Mdrawtextwidth = Mtextpaint.measuretext (Mcurrentdrawtext);            if (getprogress () = = 0) {Mdrawreachedbar = false;        Mdrawtextstart = Getpaddingleft ();            } 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);        } mdrawtextend = (int) ((GetHeight ()/2.0f)-((Mtextpaint.descent () + mtextpaint.ascent ())/2.0f)); if ((Mdrawtextstart + mdrawtextwidth) >= getwidth ()-getpaddingright ()) {Mdrawtextstart = GetWidth ()-Get            Paddingright ()-mdrawtextwidth;        Mreachedrectf.right = Mdrawtextstart-moffset;        } Float Unreachedbarstart = Mdrawtextstart + mdrawtextwidth + moffset;        if (Unreachedbarstart >= getwidth ()-getpaddingright ()) {Mdrawunreachedbar = false;            } else {Mdrawunreachedbar = true;            Munreachedrectf.left = Unreachedbarstart;            Munreachedrectf.right = GetWidth ()-getpaddingright ();            Munreachedrectf.top = GetHeight ()/2.0f +-munreachedbarheight/2.0f;        Munreachedrectf.bottom = GetHeight ()/2.0f + munreachedbarheight/2.0f; }}/** * GeT progress text color.     * * @return Progress text color.    */public int gettextcolor () {return mtextcolor;     }/** * Get progress Text size.     * * @return Progress Text size.    */Public float getprogresstextsize () {return mtextsize;    } public int Getunreachedbarcolor () {return munreachedbarcolor;    } public int Getreachedbarcolor () {return mreachedbarcolor;    } public int getprogress () {return mcurrentprogress;    } public int Getmax () {return mmaxprogress;    } public float Getreachedbarheight () {return mreachedbarheight;    } public float Getunreachedbarheight () {return munreachedbarheight;        } public void Setprogresstextsize (float textSize) {this.mtextsize = textSize;        Mtextpaint.settextsize (mtextsize);    Invalidate ();        } public void Setprogresstextcolor (int textcolor) {this.mtextcolor = TextColor; Mtextpaint.setcolor (MTextColor);    Invalidate ();        } public void Setunreachedbarcolor (int barcolor) {this.munreachedbarcolor = Barcolor;        Munreachedbarpaint.setcolor (Mreachedbarcolor);    Invalidate ();        } public void Setreachedbarcolor (int progresscolor) {this.mreachedbarcolor = Progresscolor;        Mreachedbarpaint.setcolor (Mreachedbarcolor);    Invalidate ();    } public void Setreachedbarheight (float height) {mreachedbarheight = height;    } public void Setunreachedbarheight (float height) {munreachedbarheight = height;            } public void Setmax (int maxprogress) {if (maxprogress > 0) {this.mmaxprogress = maxprogress;        Invalidate ();        }} public void Setsuffix (String suffix) {if (suffix = = null) {Msuffix = "";        } else {msuffix = suffix;    }} public String Getsuffix () {return msuffix; } public void Setprefix (String prefix) {if (prEfix = = null) Mprefix = "";        else {mprefix = prefix;    }} public String Getprefix () {return mprefix;        } public void Incrementprogressby (int by) {if (by > 0) {setprogress (getprogress () + by);        } if (Mlistener! = null) {Mlistener.onprogresschange (Getprogress (), Getmax ());            }} public void setprogress (int progress) {if (Progress <= getmax () && Progress >= 0) {            This.mcurrentprogress = progress;        Invalidate ();        }} @Override protected parcelable onsaveinstancestate () {final bundle bundle = new bundle ();        Bundle.putparcelable (Instance_state, Super.onsaveinstancestate ());        Bundle.putint (Instance_text_color, GetTextColor ());        Bundle.putfloat (Instance_text_size, Getprogresstextsize ());        Bundle.putfloat (Instance_reached_bar_height, Getreachedbarheight ()); Bundle.putfloat (instance_unreached_Bar_height, Getunreachedbarheight ());        Bundle.putint (Instance_reached_bar_color, Getreachedbarcolor ());        Bundle.putint (Instance_unreached_bar_color, Getunreachedbarcolor ());        Bundle.putint (Instance_max, Getmax ());        Bundle.putint (Instance_progress, getprogress ());        Bundle.putstring (Instance_suffix, Getsuffix ());        Bundle.putstring (Instance_prefix, Getprefix ());        Bundle.putboolean (Instance_text_visibility, getprogresstextvisibility ());    return bundle;            } @Override protected void Onrestoreinstancestate (parcelable state) {if ' (state instanceof Bundle) {            Final bundle bundle = (bundle) state;            Mtextcolor = Bundle.getint (Instance_text_color);            Mtextsize = Bundle.getfloat (instance_text_size);            Mreachedbarheight = Bundle.getfloat (instance_reached_bar_height);            Munreachedbarheight = Bundle.getfloat (instance_unreached_bar_height); Mreachedbarcolor = Bundle.getint (instance_reAched_bar_color);            Munreachedbarcolor = Bundle.getint (Instance_unreached_bar_color);            Initializepainters ();            Setmax (Bundle.getint (Instance_max));            Setprogress (Bundle.getint (instance_progress));            Setprefix (bundle.getstring (Instance_prefix));            Setsuffix (bundle.getstring (Instance_suffix)); Setprogresstextvisibility (Bundle.getboolean (instance_text_visibility)?            visible:invisible);            Super.onrestoreinstancestate (Bundle.getparcelable (instance_state));        Return    } super.onrestoreinstancestate (state);        public float dp2px (float dp) {final FLOAT scale = getresources (). Getdisplaymetrics (). density;    Return DP * scale + 0.5f;        } public float sp2px (float sp) {final float scale = getresources (). Getdisplaymetrics (). scaleddensity;    return SP * SCALE; } public void Setprogresstextvisibility (progresstextvisibility visibility) {Mifdrawtext = Visibility = = Visible;    Invalidate ();    } public boolean getprogresstextvisibility () {return mifdrawtext;    } public void Setonprogressbarlistener (Onprogressbarlistener listener) {Mlistener = listener; }}


In fact, it is very simple, there is nothing to say, interested in the code to see.

Open Code Ruyi Gate



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Android UI] Share: A simple and generous custom progress bar Numberprogressbar

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.