Use of NineOld Androids Animation Compatibility Library-ViewHelper

Posted by maheshb on Mon, 17 Jun 2019 22:14:11 +0200

This article is reproduced from: http://www.bkjia.com/Androidjc/936210.html

Function description: NineOld Androids is an open source project on github. Its function is to use attribute animation on low version android (below API 11). Its principle is also very simple, mainly to judge the current sdk version, if it is larger than API 11, then call the official API, otherwise they can achieve their own animation effect. In addition, in the use of API, it is basically the same as the official attribute animation. For example, Object Animator, Value Animator and so on.

I. Basic Use

Similar to the official, NineOld Androids mainly completes attribute animation through Object Animator, Value Animator and Animator Set.

Sample code:
1.ObjectAnimator: Move the button horizontally to the right by 100 units

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

2. Value Animator: Let the button move down 100 units vertically

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);//Be careful not to use btn.setTranslationY
    }
});

It should be noted that we use a new class called ViewHelper, which is designed to be compatible with previous apis, because methods such as setAlpha and setTranslationX are not available in low versions, so NineOld Androids provides the ViewHelper class so that we don't have to care about API versions.

3.AnimatorSet: Move 100 horizontally, 100 vertically, and the 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 Property Values Holder:

Button btn = ...
ObjectAnimator.ofPropertyValuesHolder(btn,
                PropertyValuesHolder.ofFloat("translationX",100),
                PropertyValuesHolder.ofFloat("translationY",100),
                PropertyValuesHolder.ofInt("alpha",1,0)
                ).start();


4. ViewProperty Animator: We know that the ViewProperty Animator class is provided by the government to simplify the view animation. As long as we call the animate method provided by View, we can return to the ViewProperty Animator instance. Then we can call its x, translation X, alpha, rotation and other methods to easily implement the animation. The NineOld Androids library also provides the ViewPropertyAnimator class, but in a slightly different way. It provides the static factory method animate through the ViewProperty Animator, and must bind a view in the parameter, then return to the ViewProperty Animator instance. Sample code:

Button btn = ...
ViewPropertyAnimator.animate(btn).translationX(100).translationY(100);


Note: All the classes used in the above examples should be imported into the nineold and ROIDS package!

II. ViewHelper Class Principle

ViewHelper provides a series of static set/get methods to manipulate various attributes of View, such as transparency, offset, rotation angle, etc. This greatly facilitates our use, and has the advantage of not considering the compatibility of low versions. So how does ViewHelper do that? Here we briefly analyze the principle.

Open the ViewHelper class source code, and we can find a method at random, 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 that if proxy is needed, 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 Animator Proxy:

 public static final boolean NEEDS_PROXY = Integer.valueOf(Build.VERSION.SDK).intValue() < Build.VERSION_CODES.HONEYCOMB;


Determine whether the current sdk version is below API 11, and if so, NEEDS_PROXY is 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. During the refresh process, the applyTransformation method is called to update the transparency.

@Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        //The setAlpha method causes view.invalidate to refresh, and in the refresh process, this method is called back to set transparency.
        View view = mView.get();
        if (view != null) {
            t.setAlpha(mAlpha);
            transformMatrix(t.getMatrix(), view);
        }
    }

The reason for this is that the low version does not provide setAlpha methods. If the sdk version is greater than 11, enter the else branch and call HoneyComb.setAlpha:

 static void setAlpha(View view, float alpha) {
            view.setAlpha(alpha);
        }

Other methods are similar to this.
Through the above analysis, I believe you have a deeper understanding of NineOld Androids.

Topics: SDK Attribute github Android