[Android Notes] use of NineOldAndroids animation compatibility library, nineoldandroids

Source: Internet
Author: User

[Android Notes] use of NineOldAndroids animation compatibility library, nineoldandroids
Function Description: NineOldAndroids is an open-source project on github. It is used to use property animation on android earlier than API11. Its principle is actually very simple. It mainly refers to judging the current sdk version. If it is later than API11, it will call the official API; otherwise, it will achieve its own animation effect. In addition, it is basically consistent with the official property animation in terms of API usage. Such as ObjectAnimator and ValueAnimator.
I. Basic usageLike the official website, NineOldAndroids mainly uses ObjectAnimator, ValueAnimator, and AnimatorSet to complete attribute animation.
Sample Code: 1. ObjectAnimator:

Move the button horizontally to the right in 100 units

Button btn = ...ObjectAnimator.ofFloat(btn,"translationX",100).setDuration(2000).start();

2. ValueAnimator: Move the button vertically down to 100 units
Button btn =... valueAnimator animator = ValueAnimator. ofFloat (0,100 ). setDuration (2000); animator. start (); animator. addUpdateListener (new AnimatorUpdateListener () {@ Override public void onAnimationUpdate (ValueAnimator animation) {float curVal = (float) animation. getAnimatedValue (); ViewHelper. setTranslationY (btn, curVal); // do not use btn. setTranslationY }});
We should note that we use a new class called ViewHelper, which is used to be compatible with previous APIs, because methods such as setAlpha and setTranslationX are not available in earlier versions, therefore, NineOldAndroids provides the ViewHelper class, so that we do not have to worry about the API version.
3. AnimatorSet: horizontal movement of 100, vertical movement of 100, and transparency changes.
Button btn =...AnimatorSet set = new AnimatorSet();set.playTogether(ObjectAnimator.ofFloat(btn,"translationX",100),ObjectAnimator.ofFloat(btn,"translationY",100),ObjectAnimator.ofInt(btn,"alpha",1,0));set.setDuration(2000);set.start();
Of course, this effect can also be achieved through PropertyValuesHolder:
Button btn = ...ObjectAnimator.ofPropertyValuesHolder(btn,PropertyValuesHolder.ofFloat("translationX",100),PropertyValuesHolder.ofFloat("translationY",100),PropertyValuesHolder.ofInt("alpha",1,0)).start();
4. viewPropertyAnimator: we know that the ViewPropertyAnimator class is officially provided to simplify the View animation. As long as we call the animate method provided by the View, we can return the ViewPropertyAnimator instance, then we can call its x, translationX, alpha, rotation, and other methods to conveniently implement the animation. The NineOldAndroids Library also provides the ViewPropertyAnimator class, but the usage is somewhat different. It provides the static factory method animate through ViewPropertyAnimator, and must bind a view to the parameter, and then return the ViewPropertyAnimator instance. Sample Code:
Button btn = ...ViewPropertyAnimator.animate(btn).translationX(100).translationY(100);
Note: All classes used in the preceding example should be imported into the nineoldandroids package!
Ii. Principles of ViewHelperViewHelper provides a series of static set/get methods to operate various attributes of a View, such as transparency, offset, and rotation angle, which greatly facilitates our use, the advantage is that you do not need to consider the compatibility of earlier versions. So how does ViewHelper do it? Next we will briefly analyze the principle.
Open the source code of the ViewHelper class. We can find a method, such as the setAlpha method:
 public static void setAlpha(View view, float alpha) {        if (NEEDS_PROXY) {            wrap(view).setAlpha(alpha);        } else {            Honeycomb.setAlpha(view, alpha);        }    }

This method has an internal judgment. If a proxy is required, the setAlpha method of the proxy class is called; otherwise, the setAlpha method of HoneyComb is called directly. This NEEDS_PROXY is a constant in AnimatorProxy:
 public static final boolean NEEDS_PROXY = Integer.valueOf(Build.VERSION.SDK).intValue() < Build.VERSION_CODES.HONEYCOMB;
Determine whether the current sdk version is earlier than API11. If yes, the NEEDS_PROXY is true. At this time, the if branch will enter, 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. During the refresh process, the applyTransformation method is called to update the transparency.
@ Override protected void applyTransformation (float interpolatedTime, Transformation t) {// setAlpha method will cause view. invalidate is refreshed. During the refreshing process, this method is called back to set the transparency View = mView. get (); if (view! = Null) {t. setAlpha (mAlpha); transformMatrix (t. getMatrix (), view );}}
This is because the setAlpha method is not provided in earlier versions. If the sdk version is later than 11, 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 you have a better understanding of NineOldAndroids!

Related Article

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.