original works, reproduced please specify the source: http://blog.csdn.net/qiujuer/article/details/39998961
Do Android animation effect for some time, feel deeply like the study of special effects. It's a good thing to show your own special effects on your phone.
Needless to say, the previous days I released:[Material Design] teaches you to do a Material style, animated button (Materialbutton)
In which I explained my animation implementation of the buttons for the Material effect in the Android L, today's article will be based on the advanced to explain the new effects.
The special effect principle in Materialbutton is: When the user clicks on an animation, the animation is to draw a color gradient at the click of the radius of the larger circle, so as to achieve the diffusion effect; Click the link above to see it. In the button of such effect distance Google or there is a big gap, the following to compare:
The official:
Our last version of:
It can be seen that Google is a displacement effect, and we are in situ diffusion effect, of course, animation speed of this and PS settings, do not compare, the actual speed is slightly faster than the above.
Let's try to do the effect of displacement, first draw a picture for you to see:
I believe we can understand, the first is the way before the implementation, just in situ diffusion, the second is new, will spread to the center at the same time, and in order to achieve better visual effects, the XY axis speed is not the same, the x-axis of the time = the entire diffusion time, to the y-axis time ~= The entire diffusion time is *0.3 ( and all is fast and slow), now look at the finished product effect:
Click the middle of the time with the first gap is not big, but click on both sides of the time there will be a clear gap, can feel the touch of the center closer. Is it a bit closer to Google than it is?
Having said this many theories and demos, the following is the whole realization:
First of all, we take the results of the previous article as the beginning of this article, specifically how to create a new control is no longer introduced, first look at the previous code results (the code has been modified):
public class Materialbutton extends Button {private static final interpolator Animation_interpolator = new Deceleratei Nterpolator (); Private static final long animation_time = 600; Private Paint Backgroundpaint; private static Argbevaluator Argbevaluator = new Argbevaluator (); Private float Paintx, painty, radius; Public Materialbutton (Context context) {super (context); init (null, 0); } public Materialbutton (context context, AttributeSet Attrs) {Super (context, attrs); Init (attrs, 0); } public Materialbutton (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Init (attrs, Defstyle); } @SuppressWarnings ("deprecation") private void init (AttributeSet attrs, int defstyle) {...} @SuppressWarnings ("Nullableproblems") @Override protected void OnDraw (canvas canvas) {canvas.save (); Canvas.drawcircle (Paintx, Painty, radius, backgroundpaint); Canvas.restore (); Super.ondraw (canvas); } @SuppressWarnings ("Nullableproblems") @Override public boolean ontouchevent (Motionevent event) {if (Eve Nt.getaction () = = Motionevent.action_down) {paintx = Event.getx (); Painty = Event.gety (); Startroundanimator (); } return Super.ontouchevent (event); }/** * ============================================================================================= * the Ani Mator Methods * ============================================================================================= * * /** * Start Round Animator */private void Startroundanimator () {float Start, end, height, width; Long time = (long) (Animation_time * 1.85); Height Width height = getheight (); width = getwidth (); Start End if (height < width) {start = height; end = width; } else {Start= width; end = height; } Float Startradius = (start/2 > Painty start-painty:painty) * 1.15f; float Endradius = (end/2 > Paintx end-paintx:paintx) * 0.85f; If the approximate square approximate square if (Startradius > Endradius) {Startradius = Endradius * 0.6F; Endradius = endradius/0.8f; Time = (long) (time * 0.5); } animatorset set = new Animatorset (); Set.playtogether (Objectanimator.offloat (this, Mradiusproperty, Startradius, Endradius), OBJ Ectanimator.ofobject (This, Mbackgroundcolorproperty, Argbevaluator, Attributes.getcolor (1), Attributes.getColor (2) ) ); Set Time Set.setduration ((long) (Time/end * Endradius)); Set.setinterpolator (Animation_interpolator); Set.start (); } /** * ============================================================================================= * The Custom properties * ========================================================================================= = = = * Private Property<materialbutton, float> mradiusproperty = new Property<materialbutton, FLOAT&G t; (Float.class, "radius") {@Override public Float get (Materialbutton object) {return Object.rad Ius } @Override public void Set (Materialbutton object, Float value) {Object.radius = value; Invalidate (); } }; Private Property<materialbutton, integer> mbackgroundcolorproperty = new Property<materialbutton, Integer > (Integer.class, "Bg_color") {@Override public Integer get (Materialbutton object) {return OB Ject.backgroundPaint.getColor (); } @Override public void Set (Materialbutton object, Integer value) {Object.backgroundPaint.setCol or (value); } };}In the above code we achieve the effect of diffusion on the click, the initialization of the control part because I added a lot of code here to delete, specifically can see my project implementation, finally will give the address.
Now start with this!
First we set up two new properties for the x-coordinate and y-coordinate properties, respectively:
Private Property<materialbutton, float> mpaintxproperty = new Property<materialbutton, Float> (Float.class , "Paintx") { @Override public Float Get (Materialbutton object) { return object.paintx; } @Override public void Set (Materialbutton object, Float value) { object.paintx = value; } }; Private Property<materialbutton, float> mpaintyproperty = new Property<materialbutton, Float> (Float.class , "Painty") { @Override public Float Get (Materialbutton object) { return object.painty; } @Override public void Set (Materialbutton object, Float value) { object.painty = value; } };
In both of these properties, the "invalidate ()" is not called in the first article; method to refresh the interface, because the method should be placed in the longest duration of the radius attribute called.
We then get to the aspect and calculate the corresponding starting and ending radii based on the height and width:
<span style= "White-space:pre" ></span>float start, end, height, width, speed = 0.3f; Long time = Animation_time; Height Width height = getheight (); width = getwidth (); Start End if (height < width) { start = height; end = width; } else { start = width; end = height; } Start = START/2 > Painty? Start-painty:painty; End = end * 0.8F/2F; If the approximate square approximate square if (Start > End) { start = end * 0.6F; end = end/0.8f; Time = (long) (time * 0.65); Speed = 1f; }
We first compared the height and width of the length of the short given to the beginning of the radius of the long given for the end radius.
In the second step, we divide the start length by 2 to get half of its length and then compare it with the y-coordinate when clicked, if the Y axis is longer then take y, if not enough then subtract the result. This ensures that the radius at the start of the click is just above its height or width (short side), so that there is no effect of a small round spread, and it will look like an oval effect (which will, of course, draw the ellipse directly later)
In the third step, we calculate the end radius, while guaranteeing that the end radius is half the length of the side 8/10 such effect will not appear full of the entire control situation. 8/10 of the space is just a good choice.
Fourth step, determine whether the start length is greater than the end length, if it is (approximate square case), a certain rule of the re-operation, to ensure that its starting radius is exactly the same as the length of the control (about 0.48), the end radius can just fill the control, while reducing animation time
Of course, I only now found a bug, in the second step where the bug, we look at, want to be able to propose is where the bug; The bug will be fixed in the next release.
We then create an animation of each property and set the corresponding time for each property animation:
<span style= "White-space:pre" ></span>//paintx objectanimator apaintx = ObjectAnimator.ofFloat (This , Mpaintxproperty, Paintx, WIDTH/2); Apaintx.setduration (time); Painty Objectanimator apainty = Objectanimator.offloat (this, Mpaintyproperty, Painty, HEIGHT/2); Apainty.setduration ((Long) (Time * speed)); Radius Objectanimator Aradius = Objectanimator.offloat (this, mradiusproperty, start, end); Aradius.setduration (time); Background Objectanimator abackground = Objectanimator.ofobject (this, Mbackgroundcolorproperty, Argbevaluator, Attributes.getcolor (1), Attributes.getcolor (2)); Abackground.setduration (time);
You can see the time of the y-axis multiplied by a speed variable, which by default is 0.3 if the approximate square is initialized to 1 so that it can be aligned to the center position at the same time, there is a corresponding variable in the previous step.
Then we add all the property animations to an animation set and set it to the speed: fast and slow. The animation set is finally started.
Animatorset Animatorset set = new Animatorset (); Set.playtogether (Apaintx, Apainty, Aradius, abackground); Set.setinterpolator (animation_interpolator); Set.start ();
These are the latest implementation principles and code of animation, of course, we can merge them into the first code, and use a Bool property to control which animation is used:
public class Materialbutton extends Button {private static final interpolator Animation_interpolator = new Deceleratei Nterpolator (); Private static final long animation_time = 600; Private Paint Backgroundpaint; private static Argbevaluator Argbevaluator = new Argbevaluator (); Private float Paintx, painty, radius; Private Attributes Attributes; Public Materialbutton (Context context) {super (context); init (null, 0); } public Materialbutton (context context, AttributeSet Attrs) {Super (context, attrs); Init (attrs, 0); } public Materialbutton (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Init (attrs, Defstyle); } @SuppressWarnings ("deprecation") private void init (AttributeSet attrs, int defstyle) {...} @SuppressWarnings ("Nullableproblems") @Override protected void OnDraw (canvas canvas) {canvas.save (); Canvas.drawcircle (Paintx, PaintY, Radius, backgroundpaint); Canvas.restore (); Super.ondraw (canvas); } @SuppressWarnings ("Nullableproblems") @Override public boolean ontouchevent (Motionevent event) {if (att Ributes.ismaterial () && event.getaction () = = Motionevent.action_down) {paintx = Event.getx (); Painty = Event.gety (); if (Attributes.isautomove ()) startmoveroundanimator (); else Startroundanimator (); } return Super.ontouchevent (event); }/** * ============================================================================================= * the Ani Mator Methods * ============================================================================================= * * /** * Start Round Animator */private void Startroundanimator () {float Start, end, height, width; Long time = (long) (Animation_time * 1.85); Height Width height = getheIght (); width = getwidth (); Start End if (height < width) {start = height; end = width; } else {start = width; end = height; } Float Startradius = (start/2 > Painty start-painty:painty) * 1.15f; float Endradius = (end/2 > Paintx end-paintx:paintx) * 0.85f; If the approximate square approximate square if (Startradius > Endradius) {Startradius = Endradius * 0.6F; Endradius = endradius/0.8f; Time = (long) (time * 0.5); } animatorset set = new Animatorset (); Set.playtogether (Objectanimator.offloat (this, Mradiusproperty, Startradius, Endradius), OBJ Ectanimator.ofobject (This, Mbackgroundcolorproperty, Argbevaluator, Attributes.getcolor (1), Attributes.getColor (2) ) ); Set Time Set.setduration ((long) (Time/end * Endradius)); Set.setinterPolator (Animation_interpolator); Set.start (); }/** * Start Move Round Animator */private void Startmoveroundanimator () {float Start, end, height , width, speed = 0.3f; Long time = Animation_time; Height Width height = getheight (); width = getwidth (); Start End if (height < width) {start = height; end = width; } else {start = width; end = height; } start = START/2 > Painty? Start-painty:painty; End = end * 0.8F/2F; If the approximate square approximate square if (Start > End) {start = end * 0.6F; end = end/0.8f; Time = (long) (time * 0.65); Speed = 1f; }//paintx Objectanimator Apaintx = Objectanimator.offloat (this, Mpaintxproperty, Paintx, WIDTH/2); Apaintx.setduration (time); Painty Objectanimator apainty = ObjEctanimator.offloat (This, Mpaintyproperty, Painty, HEIGHT/2); Apainty.setduration ((Long) (Time * speed)); Radius objectanimator Aradius = Objectanimator.offloat (this, mradiusproperty, start, end); Aradius.setduration (time); Background objectanimator Abackground = Objectanimator.ofobject (this, Mbackgroundcolorproperty, Argbevaluator, at Tributes.getcolor (1), Attributes.getcolor (2)); Abackground.setduration (time); Animatorset Animatorset set = new Animatorset (); Set.playtogether (Apaintx, Apainty, Aradius, Abackground); Set.setinterpolator (Animation_interpolator); Set.start (); }/** * ============================================================================================= * the CUS Tom Properties * ============================================================================================= * * Private Property<materialbutton, float> mpaintxproperty = new PROPERTY< Materialbutton, float> (Float.class, "Paintx") {@Override public Float get (Materialbutton object) { return object.paintx; } @Override public void Set (Materialbutton object, Float value) {object.paintx = value; } }; Private Property<materialbutton, float> mpaintyproperty = new Property<materialbutton, Float> (Float.class , "Painty") {@Override public Float get (Materialbutton object) {return object.painty; } @Override public void Set (Materialbutton object, Float value) {object.painty = value; } }; Private Property<materialbutton, float> mradiusproperty = new Property<materialbutton, Float> (Float.class , "radius") {@Override public Float get (Materialbutton object) {return object.radius; } @Override public void Set (Materialbutton object, Float value) {Object.radius = value; Invalidate (); } }; Private Property<materialbutton, integer> mbackgroundcolorproperty = new Property<materialbutton, Integer > (Integer.class, "Bg_color") {@Override public Integer get (Materialbutton object) {return OB Ject.backgroundPaint.getColor (); } @Override public void Set (Materialbutton object, Integer value) {Object.backgroundPaint.setCol or (value); } };}
In the last two ways after the operation of the effect comparison chart:
Isn't it good? If you feel the comparison and your appetite, here's my whole project:
Genius-android
The following will upload the corresponding test apk, of course, if everyone feels the animation is good I upload the animation video files together packaging.
[Material Design] Materialbutton Effect Advanced Animation Auto-move for alignment effect