Learning the previous two of the knowledge, for this implementation of the effect, I believe we will not feel too difficult, I want to achieve the effect of what? Please look at the following first:
It doesn't look very flashy, and its implementation is not very complex, with emphasis on drawing with the OnDraw () method.
The first is our Attrs file:
<?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= "CustomView" > <attr name= " Firstcolor "/> <attr name=" Secondcolor "/> <attr name=" Circlewidth "/> <attr name= " Speed "/> </declare-styleable> </resources>
Next is the custom view class where we rewrite the view class:
Public classMyselfcircleviewextendsView {/** First lap color*/ intFirstcolor; /** Second Circle color*/ intSecondcolor; /** Width of the circle*/ intCirclewidth; /** Rate*/ intSpeed ; /** Brushes*/Paint Mpaint; /** Progress*/ intmprogress; /** Whether to toggle flag*/ BooleanIsnext; PublicMyselfcircleview (Context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); TypedArray TypedArray= Context.gettheme (). Obtainstyledattributes (Attrs, R.styleable.customview, defstyleattr, 0); intn =Typedarray.getindexcount (); for(inti=0; i<n; i++){ intattr =Typedarray.getindex (i); Switch(attr) { CaseR.styleable.customview_firstcolor:firstcolor=Typedarray.getcolor (attr, color.red); Break; CaseR.styleable.customview_secondcolor:secondcolor=Typedarray.getcolor (attr, color.red); Break; CaseR.styleable.customview_circlewidth:circlewidth= Typedarray.getdimensionpixelsize (attr, (int) typedvalue.applydimension (typedvalue.complex_unit_px,20, Getresources (). Getdisplaymetrics ()); Break; CaseR.styleable.customview_speed:speed= Typedarray.getint (attr, 20); Break; }} typedarray.recycle (); Mpaint=NewPaint (); NewThread (NewRunnable () {@Override Public voidrun () { while(true) {mprogress++; if(Mprogress = = 360) {mprogress= 0; if(!isnext) Isnext=true; ElseIsnext=false; } postinvalidate (); Try{thread.sleep (speed); } Catch(interruptedexception e) {e.printstacktrace (); }}}). Start (); } PublicMyselfcircleview (Context context, AttributeSet attrs) { This(context, Attrs, 0); } PublicMyselfcircleview (Context context) { This(Context,NULL); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); intCentre = getwidth ()/2;//gets the x-coordinate of the center Point intradius = CENTRE-CIRCLEWIDTH/2;//radiusMpaint.setstrokewidth (Circlewidth);//set the width of the ringMpaint.setantialias (true);//Anti- aliasingMpaint.setstyle (Paint.Style.STROKE);//Set HollowRECTF Oval =NewRECTF (Centre-radius, Centre-radius, centre + RADIUS, centre + radius);//The bounds of the shape and size of the arc used to define if(!isnext) {//First Color circle complete, second color runMpaint.setcolor (Firstcolor);//set the color of the ringCanvas.drawcircle (Centre, centre, Radius, mpaint);//Draw a circleMpaint.setcolor (Secondcolor);//set the color of the ringCanvas.drawarc (oval, -90, mprogress,false, Mpaint);//draw arcs based on progress}Else{mpaint.setcolor (secondcolor);//set the color of the ringCanvas.drawcircle (Centre, centre, Radius, mpaint);//Draw a circleMpaint.setcolor (Firstcolor);//set the color of the ringCanvas.drawarc (oval, -90, mprogress,false, Mpaint);//draw arcs based on progress } } }
And finally, our layout file:
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:zhy= "Http://schemas.android.com/apk/res/com.example.myselfview"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <Com.example.myselfview.view.MySelfCircleView android:layout_width= "120DP"Android:layout_height= "120DP"Android:layout_margintop= "20DP"Android:layout_alignparenttop= "true"Android:layout_centerhorizontal= "true"Zhy:circlewidth= "15DP"Zhy:firstcolor= "#D4F668"Zhy:secondcolor= "#2F9DD2"Zhy:speed= "Ten"/> <Com.example.myselfview.view.MySelfCircleView android:layout_width= "200DP"Android:layout_height= "200DP"Android:layout_alignparentbottom= "true"Android:layout_centerhorizontal= "true"Zhy:circlewidth= "24DP"Android:layout_marginbottom= "40DP"Zhy:firstcolor= "#16A3FA"Zhy:secondcolor= "#D20F02"Zhy:speed= "5"/> </RelativeLayout>
Well, to here our effect even if the work Gaocheng, new children's shoes can be written and read, personal feeling custom view needs a lot of contact, can be used for me.
Android Custom View ring alternate wait effect