Recently did the project, used to the ProgressBar, thought to use the Android5.0 effect, it is readily realized. :
The approximate idea: 1. The circle is drawn through the canvas 2. The animation of the circle is controlled by animator.
Code: 1. Drawing the code for a circle is simple, @Overrideprotected void OnDraw (canvas canvas) {Super.ondraw (canvas);Canvas.drawarc (ARCRECTF, StartAngle + Incrementangele, SweepAngle, False, Arcpaint);if (Animatorset = = NULL | |!animatorset.isrunning ()) {Startanimation ();}}
by Canvas.draw ARC to draw the startangle is to draw the starting angle by adding incrementangle this variable is to better do animation control. 2. Animation Control Code: (This is the most important code) private void Startanimation () {if (animatorset! = null && animatorset.isrunning ()) {Animatorset.cancel (); Cancel Animation}Animatorset = new Animatorset (); Set up a collection of animationsAnimatorset set = Circuanimator (); Create a animatorset that runs a circle of animationsAnimatorset.play (set);Animatorset.addlistener (New Animatorlistener () {Private Boolean iscancel = false;@Overridepublic void Onanimationstart (Animator animation) {}@Overridepublic void Onanimationrepeat (Animator animation) {}@Overridepublic void Onanimationend (Animator animation) {if (!iscancel) {Startanimation (); Keep Looping the animation}}@Overridepublic void Onanimationcancel (Animator animation) {Iscancel = true;}}) ;Animatorset.start ();}The default animation timeprivate int defult_duration = 660;/*** Looping animations*/Private Animatorset Circuanimator () {Small Circle to Big circleValueanimator HoldAnimator1 = valueanimator.offloat (Incrementangele + defult_min_angle, Incrementangele + 115f);Holdanimator1.addupdatelistener (New Animatorupdatelistener () {@Overridepublic void Onanimationupdate (Valueanimator animation) {Incrementangele = (float) animation.getanimatedvalue ();}}) ;Holdanimator1.setduration (defult_duration);Holdanimator1.setinterpolator (New Linearinterpolator ());Valueanimator expandanimator = Valueanimator.offloat (Defult_min_angle, Defult_max_angle);Expandanimator.addupdatelistener (New Animatorupdatelistener () {@Overridepublic void Onanimationupdate (Valueanimator animation) {SweepAngle = (float) animation.getanimatedvalue ();Incrementangele-= SweepAngle;Invalidate ();}}) ;Expandanimator.setduration (defult_duration);Expandanimator.setinterpolator (New Decelerateinterpolator (2));From the big circle to the small circleValueanimator holdanimator = Valueanimator.offloat (startangle, startangle + 115f);Holdanimator.addupdatelistener (New Animatorupdatelistener () {@Overridepublic void Onanimationupdate (Valueanimator animation) {StartAngle = (float) animation.getanimatedvalue ();}});Holdanimator.setduration (defult_duration);Holdanimator.setinterpolator (New Linearinterpolator ());Valueanimator narrowanimator = Valueanimator.offloat (Defult_max_angle, Defult_min_angle);Narrowanimator.addupdatelistener (New Animatorupdatelistener () {@Overridepublic void Onanimationupdate (Valueanimator animation) {SweepAngle = (float) animation.getanimatedvalue ();Invalidate ();}}) ;Narrowanimator.setduration (defult_duration);Narrowanimator.setinterpolator (New Decelerateinterpolator (2));Animatorset set = new Animatorset ();Set.play (HoldAnimator1). with (Expandanimator);Set.play (Holdanimator). with (Narrowanimator). After (HoldAnimator1);return set;}
Ok. The realization of the idea is mainly animator animation use, a very simple way, you can practice a animator use. There's no difficulty.
NET disk Source: Http://pan.baidu.com/s/1dD71XlR
GitHub Address: Https://github.com/flyme2012/EastTrain
From for notes (Wiz)
List of attachments
Android Custom 5.0 Style ProgressBar