"Android Custom Controls" circle alternating, imitation progress effect

Source: Internet
Author: User

Or the few steps we've made to customize the view:

1. Custom View Properties

2. Get our custom properties in the View construction method

3. Rewrite onmesure (not required)

4. Rewrite OnDraw

Customizing View Properties

<?xml version= "1.0" encoding= "Utf-8"? ><resources><attr name= "Firstcolor" format= "Color"/>    <attr name= "Secondcolor" format= "color"/> <attr name=    "Circlewidth" format= "Dimension"/>    < attr name= "Speed" format= "integer"/>    <declare-styleable name= "Progressstyle" >        <attr name= " Firstcolor "/>        <attr name=" Secondcolor "/>        <attr name=" Circlewidth "/> <attr name=        " Speed "/>    </declare-styleable></resources>

Customize view, and use the custom view

public class Progressview extends View {/** * The color of the first lap */private int mfirstcolor;    /** * Color of the second lap */private int msecondcolor;    /** * Circle Width */private int mcirclewidth;    /** * Brush * */private Paint mpaint;    /** * Current Progress */private int mprogress;    /** * speed */private int mspeed;    /** * should start next */private Boolean isnext = false;    Public Progressview (Context context) {This (context, NULL);    } public Progressview (context context, AttributeSet Attrs) {This (context, attrs, 0); } public Progressview (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, Defstyleatt        R); TypedArray TypedArray = Context.gettheme (). Obtainstyledattributes (Attrs, r.styleable.progressstyle,defstyleattr,0)        ;        int n = typedarray.getindexcount ();            for (int i =0; i < n; i + +) {int attr =typedarray.getindex (i); Switch (attr) {//0,1,2 here, 3 corresponds to the order of the elements of the declare-styleable name= "Progressstyle" array in Attrs, I am For example convenience, do not write this case in actual development 0:mfir                    Stcolor = Typedarray.getcolor (attr, Color.Black);                Break                    Case 1:msecondcolor = Typedarray.getcolor (attr, color.red);                Break Case 2:mcirclewidth = Typedarray.getdimensionpixelsize (attr, (int) typedvalue.applydimension (typedvalue .                    Complex_unit_sp, 16,getresources (). Getdisplaymetrics ());                Break                    Case 3:mspeed = Typedarray.getint (attr,20);            Break        }} typedarray.recycle ();        Mpaint = new Paint ();    Startmythread ();  } @Override protected void OnDraw (canvas canvas) {INT center = getwidth ()/2;//Gets the x coordinate of the center of int radius        = (center-mcirclewidth)/2;//Radius Mpaint.setantialias (TRUE);       Mpaint.setstrokewidth (Mcirclewidth); Mpaint.setstyle (Paint.Style.STROKE); RECTF RECTF = new RECTF (Center-radius,center-radius,center+radius,center+radius);//Switch color if (!isnext) {CA            Nvas.save (); Mpaint.setcolor (Mfirstcolor);//Set the color of the ring canvas.drawcircle (center,center,radius,mpaint);//underline the Circle Mpaint            . SetColor (Msecondcolor);        Canvas.drawarc (rectf,-90,mprogress,false,mpaint);//Draw Arc Canvas.restore () according to progress;            }else {canvas.save ();            Mpaint.setcolor (Msecondcolor);//Set the color of the ring canvas.drawcircle (center,center,radius,mpaint);            Mpaint.setcolor (Mfirstcolor);            Canvas.drawarc (Rectf,-90,mprogress,false,mpaint);        Canvas.restore (); }} private void Startmythread () {New Thread (new Runnable () {@Override public void Ru N () {while (true) {mprogress++;//progress if (mprogress = = 360) {//When the value changes at 360 degrees                        is the initial statemprogress = 0;                        if (!isnext) {//sets whether to toggle the color switch Isnext = true;                        }else {isnext = false;                    }} postinvalidate ();                    try {thread.sleep (mspeed);                    }catch (interruptedexception e) {e.printstacktrace ();    }}}). Start ();; }<strong></strong>


The above code is a custom view of all the code, using the word is not much to say directly in the XML reference to the new Progressview.


Redraw OnDraw Not much explanation directly look at the code

@Override protected void OnDraw (canvas canvas) {INT center = getwidth ()/2;//Gets the x coordinate of the center of int radius = (c        Enter-mcirclewidth)/2;//Radius Mpaint.setantialias (TRUE);        Mpaint.setstrokewidth (Mcirclewidth);        Mpaint.setstyle (Paint.Style.STROKE);        RECTF RECTF = new RECTF (Center-radius,center-radius,center+radius,center+radius);            if (!isnext) {canvas.save (); Mpaint.setcolor (Mfirstcolor);//Set the color of the ring canvas.drawcircle (center,center,radius,mpaint);//underline the Circle Mpaint            . SetColor (Msecondcolor);        Canvas.drawarc (rectf,-90,mprogress,false,mpaint);//Draw Arc Canvas.restore () according to progress;            }else {canvas.save ();            Mpaint.setcolor (Msecondcolor);//Set the color of the ring canvas.drawcircle (center,center,radius,mpaint);            Mpaint.setcolor (Mfirstcolor);            Canvas.drawarc (Rectf,-90,mprogress,false,mpaint);        Canvas.restore (); }    }

The effect is not similar to progress, try it yourself



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.