Android Custom View realizes the effect of moving steps in The imitation QQ

Source: Internet
Author: User

:

1, in Attrs.xml

<declare-styleable name= "Qqstepview" >        <attr name= "outercolor" format= "color"/> <attr name=        " Innercolor "format=" color "/>        <attr name=" BorderWidth "format=" Dimension "/>        <attr name=" Stepviewtextsize "format=" Dimension "/>        <attr name=" stepviewtextcolor "format=" color "/>    </ Declare-styleable>

2, Qqstepview

 Public classQqstepviewextendsView {Private intMoutercolor =Color.gray; Private intMinnercolor =color.red; Private floatMborderwidth = 20.0f;//20 representative of 20px    Private floatMstepviewtextsize = 12.0f; Private intMstepviewtextcolor =Color.Black; PrivatePaint Moutpaint; Private intMstepmax;//the maximum value    Private intMcurrentstep;//the current value    PrivatePaint Minnerpaint; PrivatePaint Mtextpaint;  PublicQqstepview (Context context) { This(Context,NULL); }     PublicQqstepview (Context context, AttributeSet attrs) { This(context, Attrs, 0); }     PublicQqstepview (context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); //1 Analysis Effect//2 defining custom attributes, writing Attr.xml//3 using in layouts//4 Getting custom properties in a custom view//5onMeasure//6onDrawTypedArray array =context.obtainstyledattributes (Attrs, R.styleable.qqstepview); Moutercolor=Array.getcolor (R.styleable.qqstepview_outercolor, Moutercolor); Minnercolor=Array.getcolor (R.styleable.qqstepview_innercolor, Minnercolor); Mborderwidth=array.getdimension (R.styleable.qqstepview_borderwidth, mborderwidth); Mstepviewtextcolor=Array.getcolor (R.styleable.qqstepview_stepviewtextcolor, Mstepviewtextcolor); Mstepviewtextsize=array.getdimension (r.styleable.qqstepview_stepviewtextsize, mstepviewtextsize);        Array.recycle (); //Outer ARC BrushMoutpaint =NewPaint (); Moutpaint.setantialias (true);        Moutpaint.setstrokewidth (Mborderwidth);        Moutpaint.setcolor (Moutercolor); Moutpaint.setstrokecap (Paint.Cap.ROUND);//variable arc at both ends//Moutpaint.setstyle (Paint.Style.FILL);Moutpaint.setstyle (Paint.Style.STROKE); //Inner ARC BrushMinnerpaint =NewPaint (); Minnerpaint.setantialias (true);        Minnerpaint.setstrokewidth (Mborderwidth);        Minnerpaint.setcolor (Minnercolor); Minnerpaint.setstrokecap (Paint.Cap.ROUND);//variable arc at both ends//Moutpaint.setstyle (Paint.Style.FILL);Minnerpaint.setstyle (Paint.Style.STROKE); //Text BrushMtextpaint =NewPaint (); Minnerpaint.setantialias (true);        Minnerpaint.setcolor (Mstepviewtextcolor);    Mtextpaint.settextsize (mstepviewtextsize); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {        Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); //in the layout file, it may be wrap_content, possibly high-wide inconsistencies//Get mode At_most//minimum value when width height is inconsistent, hold a square        intwidth =measurespec.getsize (WIDTHMEASURESPEC); intHeight =measurespec.getsize (HEIGHTMEASURESPEC); Setmeasureddimension (Width> height? Height:width, Width > height?height:width); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); //Draw outer arc://int center = getwidth ()/2;//int radius= (int) (getwidth ()/2-MBORDERWIDTH/2);//RECTF RECTF = new RECTF (Center-radius, Center-radius, Center+radius, Center+radius);//int radius = (int) (getwidth ()/2-MBORDERWIDTH/2);RECTF RECTF =NewRECTF (MBORDERWIDTH/2, MBORDERWIDTH/2, GetWidth ()-MBORDERWIDTH/2, getwidth ()-MBORDERWIDTH/2); Canvas.drawarc (RECTF,135, 270,false, Moutpaint); //inside Arc: percent, set by user        if(Mstepmax = = 0) {            return;//prevents the first entry to 0, causing an error        }        floatSweepAngle = (float) Mcurrentstep/Mstepmax; Canvas.drawarc (RECTF,135, SweepAngle * 270,false, Minnerpaint); //Drawing TextString Steptext = Mcurrentstep + ""; Rect Textbounds=NewRect (); Mtextpaint.gettextbounds (Steptext,0, Steptext.length (), textbounds); intDX = getwidth ()/2-textbounds.width ()/2;//start position of text//BaselinePaint.fontmetricsint Fontmetricsint =Mtextpaint.getfontmetricsint (); intDY = (Fontmetricsint.bottom = fontmetricsint.top)-Fontmetricsint.bottom; intBaseLine = GetHeight ()/2 +dy;    Canvas.drawtext (steptext, DX, baseLine, mtextpaint); }    //Other, animation effects, etc.     Public synchronized voidSetstepmax (intStepmax) {         This. Mstepmax =Stepmax; }    //synchronized to prevent multi-threaded operation errors     Public synchronized voidSetcurrentstep (intcurrentstep) {         This. Mcurrentstep =Currentstep; //constantly drawinginvalidate (); }}

3. In the Activity layout

<com.siberiadante.view.QQStepView        android:id= "@+id/step_view"        android:layout_width = "Match_parent"        android:layout_height= "Match_parent"        android:layout_centerinparent = "true"        app:borderwidth= "10DP"        app:innercolor= "@color/blue"        app:o Utercolor= "@color/coloraccent"        app:stepviewtextcolor= "@color/blue"        app: Stepviewtextsize= "28sp"/>

4. In the activity

Mtvstart =(TextView) Findviewbyid (R.id.tv_start); Stepview=(Qqstepview) Findviewbyid (R.id.step_view); Stepview.setstepmax (10000);//Stepview.setcurrentstep (5000); //Property Animations        FinalValueanimator valueanimator = objectanimator.offloat (0, 9000); Valueanimator.setduration (3000); Valueanimator.addupdatelistener (NewValueanimator.animatorupdatelistener () {@Override Public voidonanimationupdate (Valueanimator valueanimator) {floatValue = (float) Valueanimator.getanimatedvalue (); Stepview.setcurrentstep ((int) value);        }        }); Mtvstart.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Valueanimator.start ();    }        }); }

My public number: Tstongxiao

GitHub Address: https://github.com/SiberiaDante/SiberiaDanteLib/blob/master/siberiadante/src/main/java/com/ Siberiadante/view/qqstepview.java

Reference article: Http://www.jianshu.com/p/4e0eb9bb09ab

Android Custom View realizes the effect of moving steps in The imitation QQ

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.