Android Custom Circular button click Progress Animation

Source: Internet
Author: User

One months ago when I went to the interview, I was asked a similar question, when I did not answer, I went home to find the following information: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0325/1603.html,
According to the above link, I changed the next half an hour later to achieve a relatively rough effect, such as, not very good forgive ha ~

Specifically decomposed into 4 parts:
The bottom of a gray hollow stroke ring, the middle of a text, 2 sides of a semicircle to do upward animation;

Specific code:

 Public  class circlebartwosider extends View {    PrivateRECTF Mcolorwheelrectangle =NewRECTF ();//Rectangle range of the Circle    PrivatePaint Mdefaultwheelpaint;//Draw brush for bottom Gray circle    PrivatePaint Mcolorwheelpaintleft;//Draw the blue sector to the left of the brush    PrivatePaint Mcolorwheelpaintright;//Draw the blue sector to the right of the brush    PrivatePaint Textpaint;//Brush for middle text    Private floatMcolorwheelradius;//Circle radius in normal state    Private floatCirclestrokewidth;//The line thickness of the circle    Private floatPressextrastrokewidth;//Press to increase the thickness of the added Circle line    PrivateString MText;//Middle text content    Private intMCount;//In order to achieve the number increase effect and add the variable, he and mtext actually represent a meaning    Private floatMsweepangleper;//In order to achieve the blue fan increase effect and add the variable, he and msweepangle actually represent a meaning    Private floatMsweepangle;//Sector Radian    Private intMtextsize;//Text colorBaranimation Anim;//Animation class    Private intTime = -; Public Circlebartwosider(Context context) {Super(context); InitNULL,0); } Public Circlebartwosider(context context, AttributeSet attrs) {Super(context, attrs); Init (Attrs,0); } Public Circlebartwosider(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle);    Init (attrs, Defstyle); }Private void Init(AttributeSet Attrs,intDefstyle) {circlestrokewidth = myutils.dip2px (GetContext (),Ten); Pressextrastrokewidth = myutils.dip2px (GetContext (),2); Mtextsize = myutils.dip2px (GetContext (), -); Mcolorwheelpaintright =NewPaint (Paint.anti_alias_flag); Mcolorwheelpaintright.setcolor (0xff29a6f6);        Mcolorwheelpaintright.setstyle (Paint.Style.STROKE); Mcolorwheelpaintright.setstrokemiter (0);//Mcolorwheelpaintright.setstrokecap (Paint.Cap.ROUND);//Turn on the display edge as a circleMcolorwheelpaintright.setstrokewidth (Circlestrokewidth); Mcolorwheelpaintleft =NewPaint (Paint.anti_alias_flag); Mcolorwheelpaintleft.setcolor (0xff29a6f6);        Mcolorwheelpaintleft.setstyle (Paint.Style.STROKE); Mcolorwheelpaintleft.setstrokemiter (0);//Mcolorwheelpaintleft.setstrokecap (Paint.Cap.ROUND);//Turn on the display edge as a circleMcolorwheelpaintleft.setstrokewidth (Circlestrokewidth); Mdefaultwheelpaint =NewPaint (Paint.anti_alias_flag); Mdefaultwheelpaint.setcolor (0XFFEEEFEF);        Mdefaultwheelpaint.setstyle (Paint.Style.STROKE);        Mdefaultwheelpaint.setstrokewidth (Circlestrokewidth); Textpaint =NewPaint (Paint.anti_alias_flag |        Paint.linear_text_flag); Textpaint.setcolor (0xff333333);        Textpaint.setstyle (Style.fill_and_stroke);        Textpaint.settextalign (Align.left);        Textpaint.settextsize (mtextsize); MText ="0"; Msweepangle =1; Anim =NewBaranimation (); Anim.setduration (time);//Set animations often}@Override    protected void OnDraw(Canvas canvas) {//TODO auto-generated method stub        Super. OnDraw (canvas); Canvas.drawarc (Mcolorwheelrectangle,- -, the,false, Mdefaultwheelpaint); Canvas.drawarc (Mcolorwheelrectangle, -, Msweepangleper,false, mcolorwheelpaintright); Canvas.drawarc (Mcolorwheelrectangle, -,-msweepangleper,false, Mcolorwheelpaintleft); Rect bounds =NewRect (); String textstr="can buy:"+mcount+""; Textpaint.gettextbounds (TEXTSTR,0, Textstr.length (), bounds); Canvas.drawtext (textstr+"", (Mcolorwheelrectangle.centerx ())-(Textpaint.measuretext (TEXTSTR)/2), Mcolorwheelrectangle.centery () + bounds.height ()/2, Textpaint); }@Override    protected void onmeasure(intWidthmeasurespec,intHEIGHTMEASURESPEC) {//TODO auto-generated method stub        intHeight = getdefaultsize (getsuggestedminimumheight (), heightmeasurespec);intwidth = getdefaultsize (Getsuggestedminimumwidth (), widthmeasurespec);intmin = math.min (width, height);        Setmeasureddimension (min, min);        Mcolorwheelradius = Min-circlestrokewidth-pressextrastrokewidth;                Mcolorwheelrectangle.set (Circlestrokewidth+pressextrastrokewidth, Circlestrokewidth+pressextrastrokewidth,    Mcolorwheelradius, Mcolorwheelradius); }@Override     Public void setpressed(BooleanPressed) {//TODO auto-generated method stubLOG.I ("Hloong","Call setpressed");if(pressed) {Mcolorwheelpaintleft.setcolor (0xff165da6); Mcolorwheelpaintright.setcolor (0xff165da6); Textpaint.setcolor (0xff070707);            Mcolorwheelpaintleft.setstrokewidth (Circlestrokewidth+pressextrastrokewidth);            Mcolorwheelpaintright.setstrokewidth (Circlestrokewidth+pressextrastrokewidth);            Mdefaultwheelpaint.setstrokewidth (Circlestrokewidth+pressextrastrokewidth);        Textpaint.settextsize (Mtextsize-pressextrastrokewidth); }Else{Mcolorwheelpaintleft.setcolor (0xff29a6f6); Mcolorwheelpaintright.setcolor (0xff29a6f6); Textpaint.setcolor (0xff333333);            Mcolorwheelpaintleft.setstrokewidth (Circlestrokewidth);            Mcolorwheelpaintright.setstrokewidth (Circlestrokewidth);            Mdefaultwheelpaint.setstrokewidth (Circlestrokewidth);        Textpaint.settextsize (mtextsize); }Super. setpressed (pressed); This. Invalidate (); } Public void startcustomanimation(){ This. Startanimation (Anim); } Public void SetText(String text) {mText = text; This. Startanimation (Anim); } Public void Setsweepangle(floatSweepAngle) {msweepangle = SweepAngle; }/** * Animation * @author long * 2015-3-20 PM 2:11:22 * *     Public  class baranimation extends Animation {        /** * Initializes expand collapse animation, has both types, collapse (1) and expand (0). * @param View the view to animate * @param Type the type of animation:0 would expand from gone a         nd 0 size to visible and layout of size defined in XML. * 1 'll collapse view and set to Gone * Animation class takes advantage of the change characteristics of the Interpolatedtime parameter (from 0 to 1) in the Applytransformation parameter, * implements A property of the view changes over time. The principle is that each time the system calls animation's Applytransformation () method, * Change the value of the Msweepangleper,mcount, * and then call Postinvalidate () to draw the view continuously         。 */         Public baranimation() {        }@Override        protected void applytransformation(floatInterpolatedtime, transformation t) {Super. applytransformation (Interpolatedtime, T);//msweepangleper,mcount These two properties are just temporary properties to be used during the animation process .            //mtext and Msweepangle are the true values of the sector radians and median values after the end of the animation.             if(Interpolatedtime <1.0f) {msweepangleper = Interpolatedtime * Msweepangle; MCount = (int) (Interpolatedtime * float.parsefloat (MText)); }Else{msweepangleper = Msweepangle;            MCount = Integer.parseint (MText);        } postinvalidate (); }    }}

Activity's Code:

 Public  class mainactivity extends Activity {    PrivateCirclebartwosider Circlebar;PrivateCirclebar circleBar2;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Circlebar = (circlebartwosider) Findviewbyid (r.id.circle); Circlebar.setsweepangle ( -);//Set angleCirclebar.settext ("27000"); Circlebar.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View view)            {circlebar.startcustomanimation ();        }        });        CircleBar2 = (Circlebar) Findviewbyid (r.id.circle2); Circlebar2.setsweepangle ( -); Circlebar2.settext ("a"); Circlebar2.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View view)            {circlebar2.startcustomanimation ();    }        }); }}

Project: Https://github.com/hloong/CricleBarDemo

Android Custom Circular button click Progress Animation

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.