Custom loading the use of the loading view animation component.

Source: Internet
Author: User
Tags getcolor

Find a little cool loading animation on GitHub https://github.com/Fichardu/CircleProgress

I write the use steps

Code for Custom View (circleprogress)

 PackageCom.hysmarthotel.view;ImportCOM.HYSMARTHOTEL.ROOMCONTROL.R;ImportCom.hysmarthotel.util.EaseInOutCubicInterpolator;ImportAndroid.animation.TimeInterpolator;ImportAndroid.content.Context;ImportAndroid.content.res.TypedArray;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.graphics.Point;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;Importandroid.view.animation.AnimationUtils; Public classCircleprogressextendsView {Private Static Final intRED = 0xffe5282c; Private Static Final intYELLOW = 0xff1f909a; Private Static Final intBLUE = 0xfffc9e12; Private Static Final intColor_num = 3; Private int[] COLORS; PrivateTimeinterpolator Minterpolator =NewEaseinoutcubicinterpolator (); Private Final Doubledegree = math.pi/180; PrivatePaint Mpaint; Private intmviewsize; Private intMpointradius; Private LongMstarttime; Private LongMplaytime; Private BooleanMstartanim =false; PrivatePoint mcenter =NewPoint (); Privatearcpoint[] Marcpoint; Private Static Final intPoint_num = 15; Private Static Final intDelta_angle = 360/Point_num; Private LongMduration = 3600;  Publiccircleprogress (Context context) {Super(context); Init (NULL, 0); }     Publiccircleprogress (Context context, AttributeSet attrs) {Super(context, attrs); Init (Attrs,0); }     PublicCircleprogress (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle);    Init (attrs, Defstyle); }    Private voidInit (AttributeSet attrs,intDefstyle) {Marcpoint=NewArcpoint[point_num]; Mpaint=NewPaint (); Mpaint.setantialias (true);        Mpaint.setstyle (Paint.Style.FILL); TypedArray a= GetContext (). Obtainstyledattributes (Attrs, r.styleable.circleprogress, Defstyle, 0); intColor1 =A.getcolor (R.styleable.circleprogress_color1, RED); intColor2 =A.getcolor (R.styleable.circleprogress_color2, YELLOW); intColor3 =A.getcolor (R.styleable.circleprogress_color3, BLUE);        A.recycle (); COLORS=New int[]{color1, Color2, Color3}; } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {        intDefaultSize =getresources (). Getdimensionpixelsize (R.dimen.default_circle_view_size); intwidth =getdefaultsize (DefaultSize, Widthmeasurespec); intHeight =getdefaultsize (DefaultSize, Heightmeasurespec); Mviewsize=math.min (width, height);        Setmeasureddimension (Mviewsize, mviewsize); Mcenter.set (Mviewsize/2, MVIEWSIZE/2); Calpoints (1.0f); } @Overrideprotected voidOnDraw (canvas canvas) {canvas.save ();        Canvas.translate (mcenter.x, MCENTER.Y); floatFactor =Getfactor (); Canvas.rotate (36 *factor); floatx, y;  for(inti = 0; i < Point_num; ++i) {mpaint.setcolor (marcpoint[i].color); floatItemfactor =Getitemfactor (I, factor); X= Marcpoint[i].x-2 * marcpoint[i].x *Itemfactor; Y= Marcpoint[i].y-2 * MARCPOINT[I].Y *Itemfactor;        Canvas.drawcircle (x, Y, Mpointradius, mpaint);        } canvas.restore (); if(Mstartanim) {postinvalidate (); }    }    Private voidCalpoints (floatfactor) {        intRadius = (int) (MVIEWSIZE/3 *factor); Mpointradius= RADIUS/12;  for(inti = 0; i < Point_num; ++i) {floatx = Radius *-(float) Math.sin (degree * delta_angle *i); floaty = radius *-(float) Math.Cos (degree * delta_angle *i); Arcpoint Point=NewArcpoint (x, y, colors[i%Color_num]); Marcpoint[i]=Point ; }    }    Private floatGetfactor () {if(Mstartanim) {mplaytime= Animationutils.currentanimationtimemillis ()-Mstarttime; }        floatFactor = Mplaytime/(float) mduration; returnFactor%1f; }    Private floatGetitemfactor (intIndexfloatfactor) {        floatItemfactor = (Factor-0.66f/point_num * index) * 3; if(Itemfactor <0f) {itemfactor=0f; } Else if(Itemfactor >1f) {Itemfactor=1f; }        returnminterpolator.getinterpolation (Itemfactor); }     Public voidStartanim () {mplaytime= mplaytime%mduration; Mstarttime= Animationutils.currentanimationtimemillis ()-Mplaytime; Mstartanim=true;    Postinvalidate (); }     Public voidReset () {Stopanim (); Mplaytime= 0;    Postinvalidate (); }     Public voidStopanim () {Mstartanim=false; }     Public voidsetinterpolator (Timeinterpolator interpolator) {minterpolator=Interpolator; }     Public voidSetduration (Longduration) {mduration=duration; }     Public voidSetradius (floatfactor)        {Stopanim ();        Calpoints (factor);    Startanim (); }    Static classArcpoint {floatx; floaty; intcolor; Arcpoint (floatXfloatYintcolor) {             This. x =x;  This. y =y;  This. color =color; }    }}

Easeinoutcubicinterpolator is a tool used in Custom view (circleprogress)

 PackageCom.hysmarthotel.util;ImportAndroid.animation.TimeInterpolator; Public classEaseinoutcubicinterpolatorImplementsTimeinterpolator {@Override Public floatGetinterpolation (floatinput) {        if(Input *= 2) < 1.0f) {            return0.5f * Input * input *input; } Input-= 2; return0.5f * Input * input * input + 1; }}

Calls in activity (there are other uses to see the source code on GitHub yourself)

Mprogressview = (circleprogress) Findviewbyid (R.id.progress_vie); Mprogressview.startanim ();  Start Mprogressview.stopanim ();  End Mprogressview.setradius (factor);  Radius mprogressview.reset ();  Recovery

Layout in the XML file

<?xml version= "1.0" encoding= "Utf-8"?><absolutelayout xmlns:android= "Http://schemas.android.com/apk/res/android"xmlns:circleprogress= "Http://schemas.android.com/apk/res/com.hysmarthotel.roomcontrol"//This place remember to add//package nameAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@drawable/bg1" > <com.hysmarthotel.view.CircleProgress//class name Android:id= "@+id/progress_vie"android:layout_x= "350.5px"android:layout_y= "150.0px"Android:layout_width= "1140.0px"Android:layout_height= "700.0px"Circleprogress:color1= "@android: color/holo_red_light"//These parameters are xmlns:circleprogress, and attrs files associated with the
Circleprogress:color2= "@android: Color/holo_green_light"

Circleprogress:color3= "@android: Color/holo_blue_light"/>

You create a new Attrs file in the values directory, which is related to custom parameters in the custom view

    <declare-styleable name= "circleprogress" >        <attr name= "Color1" format= "Reference|color"/>        < attr name= "Color2" format= "Reference|color"/>        <attr name= "Color3" format= "Reference|color"/>    < /declare-styleable>

Create your own Dimens file in the values directory, this is just a few color parameters

<?xml version= "1.0" encoding= "Utf-8"?><resources> <dimen    name= "Activity_horizontal_margin" > 16dp</dimen>    <dimen name= "Activity_vertical_margin" >16dp</dimen>    <dimen name= " Default_circle_view_size ">200dp</dimen></resources>

Custom loading the use of the loading view animation component.

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.