Feature Description:Nineoldandroids is an open source project on GitHub that is designed to use property animations on low-version Android (API11 below). Its principle is actually very simple, mainly is to judge the current version of the SDK, if more than API11, then call the official API, otherwise you realize the animation effect. Other than thatin terms of API usage, it is basically consistent with official property animations. such as Objectanimator, Valueanimator and so on.
I. Basic USEsimilar to the official, nineoldandroids mainly through the Objectanimator, Valueanimator, Animatorset to complete the property animation.
Example code:1.ObjectAnimator:
Move the button horizontally to the right by 100 units
Button btn = ... Objectanimator.offloat (BTN, "Translationx", +). Setduration (+). Start ();
2.ValueAnimator: Let the button move vertically down 100 units
Button btn = ... Valueanimator animator = Valueanimator.offloat (0,100). Setduration (+); Animator.start (); Animator.addupdatelistener (New Animatorupdatelistener () {@Override public void Onanimationupdate (valueanimator Animation) {Float curval = (float) animation.getanimatedvalue (); Viewhelper.settranslationy (btn,curval);//Take care not to use btn.settranslationy}});
You should notice here that we are using a new class called Viewhelper, which is designed to be compatible with the previous API, because methods like Setalpha,settranslationx are not available in the lower version. So Nineoldandroids provides the Viewhelper class so that we don't have to care about the API version.
3.AnimatorSet:moves horizontally 100, moves vertically 100, and the transparency changes.
Button btn = ... Animatorset set = new Animatorset (), Set.playtogether (Objectanimator.offloat (btn, "Translationx", 100), Objectanimator.offloat (BTN, "translationy", +), Objectanimator.ofint (BTN, "alpha", 1,0)); Set.setduration (2000); Set.start ();
of course, this effect can also be achieved by Propertyvaluesholder:
Button btn = ... Objectanimator.ofpropertyvaluesholder (Btn,propertyvaluesholder.offloat ("Translationx", 100), Propertyvaluesholder.offloat ("Translationy", +), Propertyvaluesholder.ofint ("Alpha", 1,0)). Start ();
4.ViewPropertyAnimator: We know that the official offer of the Viewpropertyanimator class simplifies the view animation, As long as we invoke the Animate method provided by view to return the Viewpropertyanimator instance, then we can invoke its X, Translationx, alpha, rotation and other methods to implement the animation easily. The Nineoldandroids library also provides the Viewpropertyanimator class, but it's a bit different in how it's used. It provides a static factory method through Viewpropertyanimator animate, and must bind a view in the parameter and then return the Viewpropertyanimator instance. Example code:
Button btn = ... Viewpropertyanimator.animate (BTN). Translationx (+). Translationy (100);
Note: The classes used in the example above should all be imported into the Nineoldandroids package!
two. Viewhelper class principleViewhelper provides a series of static set/get methods to manipulate the various properties of the view, such as transparency, offsets, rotation angles, and so on, which greatly facilitates our use, and has the advantage of not having to consider the compatibility issues of the lower version. So how did viewhelper do it? Below we briefly analyze the principle.
Open Viewhelper Class Source code, we find a way, such as Setalpha method:
public static void Setalpha (view view, float Alpha) { if (needs_proxy) { wrap (view). Setalpha (Alpha); } else { honeycomb.setalpha (view, Alpha); } }
There is a judgment inside this method, if you need a proxy, then call the Setalpha method of the proxy class, otherwise call Honeycomb's Setalpha method directly. This needs_proxy is a constant in the Animatorproxy:
public static Final Boolean needs_proxy = Integer.valueof (Build.VERSION.SDK). Intvalue () < Build.version_codes. Honeycomb;
determines whether the current SDK version is API11 or not, and if so, Needs_proxy true. At this point, you enter the IF branch, call the Wrap method in the Animatorproxy class, wrap the view, and then call the Setalpha method to set the transparency:
public void Setalpha (float alpha) { if (malpha! = Alpha) { malpha = Alpha; View view = Mview.get (); if (view! = null) { view.invalidate ();}} }
you can see that this method sets the Malpha variable and calls the Invalidate method to refresh the view, which calls the Applytransformation method to update the transparency during the refresh process.
@Override protected void applytransformation (float interpolatedtime, transformation t) { // The Setalpha method causes the View.invalidate to refresh, and during the refresh process, this method is invoked to set the transparency of view view = Mview.get (); if (view! = null) { t.setalpha (malpha); Transformmatrix (T.getmatrix (), view); } }
This is done because the Setalpha method is not available in the low version. If the SDK version is greater than 11, then go to the Else branch and call Honeycomb.setalpha:
static void Setalpha (view view, float Alpha) { view.setalpha (alpha); }
Other methods are similar to this one.
Through the above analysis, I believe we have a deeper understanding of nineoldandroids!
The use of the "Android Notes" Nineoldandroids Animation compatibility library